Button.OnClickListener 객체를 생성하여 .버튼에 설정 해주는 방식입니다.
버튼 오브젝트를 생성하고
Button.OnClickListener btnObject = new View.OnClickListener(){public void onClick(View v){}};
이걸 callback으로 이용해서 버튼을 정의 합니다.
1. Layout에 이벤트 추가
activity_main.xml에 Button의 Layout을 구성 합니다.
<Button
android:id="@+id/btn_object1"
android:text="@string/btn_object1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
3개의 버튼을 만들어서 테스트를 진행 합니다..
2. Activity에 기능 구현
Button.OnClickListener 객체를 생성 하고 각 버튼 클릭시 간단한 메시지를 보여주도록 합니다.
Button.OnClickListener btnObject = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId() == R.id.btn_object1) {
Toast.makeText(MainActivity.this, "Object Button1", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_object2) {
Toast.makeText(MainActivity.this, "Object Button2", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_object3) {
Toast.makeText(MainActivity.this, "Object Button3", Toast.LENGTH_SHORT).show();
}
}
};
onCreate()에서 버튼에 생성된 Button.OnClickListener 객체를 대입해 줍니다.
findViewById(R.id.btn_object1).setOnClickListener(btnObject);
findViewById(R.id.btn_object2).setOnClickListener(btnObject);
findViewById(R.id.btn_object3).setOnClickListener(btnObject);
3. 결과
버튼 3개가 구현되고 선택을 하면 해당 메시지가 나타납니다.
4. Source Code
4.1 activity_main.xml
버튼 3개를 구현 합니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_object1"
android:text="@string/btn_object1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_object2"
android:text="@string/btn_object2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_object3"
android:text="@string/btn_object3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
4.2 strings.xml
button에 사용되는 text 내용 입니다.
<resources>
<string name="app_name">Button Object</string>
<string name="btn_object1">Object1</string>
<string name="btn_object2">Object2</string>
<string name="btn_object3">Object3</string>
</resources>
4.3 MainActivity.java
package copycoding.button.object.buttonobject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Button.OnClickListener btnObject = new View.OnClickListener(){public void onClick(View v){}};
// 4. OnClickListener를 객체로 만드는 방법
Button.OnClickListener btnObject = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId() == R.id.btn_object1) {
Toast.makeText(MainActivity.this, "Object Button1", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_object2) {
Toast.makeText(MainActivity.this, "Object Button2", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_object3) {
Toast.makeText(MainActivity.this, "Object Button3", Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 4. OnClickListener를 객체로 만드는 방법
findViewById(R.id.btn_object1).setOnClickListener(btnObject);
findViewById(R.id.btn_object2).setOnClickListener(btnObject);
findViewById(R.id.btn_object3).setOnClickListener(btnObject);
}
}
다른 버튼 사용 관련 참조
[android] 안드로이드 버튼(1) onClick() 함수 사용 방법
[android] 안드로이드 버튼(2) 생성시 OnClickListener 구현 방법
[android] 안드로이드 버튼(3)
OnClickListener 인터페이스 구현 방법
[android] 안드로이드 버튼(5) 버튼 생성 모음 및 이미지 버튼
- copy coding -
'Android' 카테고리의 다른 글
| [안드로이드 스튜디오] 핸드폰 드라이버 설치 방법 (0) | 2018.12.10 |
|---|---|
| [android] 안드로이드 버튼(5) 버튼 생성 모음 및 이미지 버튼 (0) | 2018.12.06 |
| [android] 안드로이드 버튼(3) OnClickListener 인터페이스 구현 방법 (0) | 2018.12.06 |
| [android] 안드로이드 버튼(2) 생성시 OnClickListener 구현 방법 (0) | 2018.12.06 |
| [android] 안드로이드 버튼(1) onClick() 함수 사용 방법 (0) | 2018.12.06 |