사악미소
현대마법의 공방
사악미소
전체 방문자
오늘
어제
  • 분류 전체보기
    • Document
    • JavaScript
    • jQuery
    • Spring Web
      • Spring Framework
      • Spring Boot
    • Android
      • Java Code
      • Setting
    • iOS
      • Swift Code
      • Setting
    • PHP
      • Code
      • Setting
    • AWS
    • LINUX
      • Rocky Linux
      • CentOS
    • Node.js
    • Developer Tool
    • GIT
    • MAC
    • Scraping Document
    • MariaDB
    • WYSIWYG
    • Scouter
    • Docker
    • Planning
    • 용어정리
반응형

인기 글

최근 댓글

Programmer 사악미소.
사악미소

사악미소의 현대마법의 공방

[Android] setOnClickListener(this)를 이용한 버튼 클릭 이벤트
Android/Java Code

[Android] setOnClickListener(this)를 이용한 버튼 클릭 이벤트

2022. 11. 9. 16:13
728x90

 

 

 

 

[Android] ViewBinding 기법

ViewBinding 기법은 레이아웃 XML 파일에 선언된 뷰 객체를 코드에서 쉽게 이용할 수 있도록 한다. 레이아웃 XML 파일에 선언된 뷰 객체를 코드에서 이용하기 위해서 뷰의 식별자나 ID 속성을 등록한

saakmiso.tistory.com

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="3">

<Button
    android:id="@+id/btn_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_margin="2dp"
    android:text="Button1" />

<Button
    android:id="@+id/btn_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="0"
    android:layout_column="1"
    android:layout_margin="2dp"
    android:text="Button2" />

<Button
    android:id="@+id/btn_three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="0"
    android:layout_column="2"
    android:layout_margin="2dp"
    android:text="Button3" />

</GridLayout>

 

 

 

안드로이드 Activity의 버튼 이벤트 정리를 위해 사용

 

package org.example.cleanbutton;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.example.cleanbutton.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // 뷰 바인딩 객체 획득
        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // OnClickListener interface를 implements하여 메서드 구현
        binding.btnOne.setOnClickListener(this);
        binding.btnTwo.setOnClickListener(this);
        binding.btnThree.setOnClickListener(this);
    }

    // View.OnClickListener를 implements하므로 onClick() 함수를 오버라이딩
    @Override
    public void onClick(View view) {

        switch(view.getId()) {
            case R.id.btn_one :
                Toast.makeText(getApplicationContext(), "버튼01 클릭했습니다.", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_two :
                Toast.makeText(getApplicationContext(), "버튼02 클릭했습니다.", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_three :
                Toast.makeText(getApplicationContext(), "버튼03 클릭했습니다.", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}

 

728x90

'Android > Java Code' 카테고리의 다른 글

[Android] Activity에서 Dark Theme 비활성화기  (0) 2023.01.04
[Android] 갤러리 이미지 가져오기 및 썸네일 생성  (0) 2022.11.10
[Android] 카메라 사진 가져오기 및 썸네일 생성  (0) 2022.11.10
[Android] Application 설치 여부 및 확인  (0) 2022.08.05
[Android] 액션바에 검색기능 활성화  (0) 2022.07.19
    'Android/Java Code' 카테고리의 다른 글
    • [Android] 갤러리 이미지 가져오기 및 썸네일 생성
    • [Android] 카메라 사진 가져오기 및 썸네일 생성
    • [Android] Application 설치 여부 및 확인
    • [Android] 액션바에 검색기능 활성화
    사악미소
    사악미소

    티스토리툴바