버튼의 OnClickListener는 View.OnClickListener의 정의을 보면 다음과 같이 interface인 것을 알 수 있습니다.
public static interface View.OnClickListener
이걸 implements 받아 버튼 클릭 이벤트에 대한 인터페이스를 구현하는 방법 입니다.
1. Layout에 이벤트 추가
activity_main.xml에 Button의 Layout을 구성 합니다.
<Button
android:id="@+id/btn_implements1"
android:text="@string/btn_implement1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
동일한 형태의 버튼 3개를 만들어서 테스트를 진행 합니다..
2. Activity에 기능 구현
먼저 Activity class를 생성할 때 OnClickListener를 implements 받습니다.
public class MainActivity extends AppCompatActivity
implements View.OnClickListener // 3. implements 구현 방법
onCreate()에서 버튼을 생성해 줍니다.
// 3. MainActivity에 implements View.OnClickListener를 이용한 경우
Button btnImplement1 = (Button)findViewById(R.id.btn_implements1);
Button btnImplement2 = (Button)findViewById(R.id.btn_implements2);
Button btnImplement3 = (Button)findViewById(R.id.btn_implements3);
btnImplement1.setOnClickListener(this);
btnImplement2.setOnClickListener(this);
btnImplement3.setOnClickListener(this);
OnClickListener의 onClick(View v)을 Overide 하여 필요한 작업을 구현 합니다.
// 3. MainActivity에 implements View.OnClickListener를 이용한 경우
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_implements1) {
Toast.makeText(MainActivity.this, "implements Button1", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_implements2) {
Toast.makeText(MainActivity.this, "implements Button2", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_implements3) {
Toast.makeText(MainActivity.this, "implements Button3", Toast.LENGTH_SHORT).show();
}
}
getId()를 이용하여 어떤 버튼이 클릭 되었는지 확인을 하고
각 버튼 클릭시 Toast로 간단한 메시지가 나타 나도록 했습니다.
3. 결과
버튼 3개가 구현되고 버튼 클릭시 메시지가 나타납니다.
4. Source Code
4.1 activity_main.xml
화면 구성 layout에 버튼 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_implements1"
android:text="@string/btn_implement1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_implements2"
android:text="@string/btn_implement2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="125dp"
android:layout_marginTop="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_implements3"
android:text="@string/btn_implement3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="245dp"
android:layout_marginTop="150dp"
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 Implements</string>
<string name="btn_implement1">Implement1</string>
<string name="btn_implement2">Implement2</string>
<string name="btn_implement3">Implement3</string>
</resources>
4.3 MainActivity.java
버튼을 생성 하고 클릭 이벤트를 구현 합니다.
package copycoding.tistory.buttonimplements;
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
implements View.OnClickListener // 3. implements 구현 방법
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 3. MainActivity에 implements View.OnClickListener를 이용한 경우
Button btnImplement1 = (Button)findViewById(R.id.btn_implements1);
Button btnImplement2 = (Button)findViewById(R.id.btn_implements2);
Button btnImplement3 = (Button)findViewById(R.id.btn_implements3);
btnImplement1.setOnClickListener(this);
btnImplement2.setOnClickListener(this);
btnImplement3.setOnClickListener(this);
}
// 3. MainActivity에 implements View.OnClickListener를 이용한 경우
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_implements1) {
Toast.makeText(MainActivity.this, "implements Button1", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_implements2) {
Toast.makeText(MainActivity.this, "implements Button2", Toast.LENGTH_SHORT).show();
} else if(v.getId() == R.id.btn_implements3) {
Toast.makeText(MainActivity.this, "implements Button3", Toast.LENGTH_SHORT).show();
}
}
}
다른 버튼 사용 관련 참조
[android] 안드로이드 버튼(1) onClick() 함수 사용 방법
[android] 안드로이드 버튼(2) 생성시 OnClickListener 구현 방법
[android] 안드로이드 버튼(4) OnClickListener를 객체로 선언하여 구현하는 방법
[android] 안드로이드 버튼(5) 버튼 생성 모음 및 이미지 버튼
- copy coding -
'Android' 카테고리의 다른 글
| [android] 안드로이드 버튼(5) 버튼 생성 모음 및 이미지 버튼 (0) | 2018.12.06 |
|---|---|
| [android] 안드로이드 버튼(4) OnClickListener를 객체로 선언하여 구현하는 방법 (0) | 2018.12.06 |
| [android] 안드로이드 버튼(2) 생성시 OnClickListener 구현 방법 (0) | 2018.12.06 |
| [android] 안드로이드 버튼(1) onClick() 함수 사용 방법 (0) | 2018.12.06 |
| [안드로이드] emulator: ERROR: x86 emulation currently requires hardware acceleration! (0) | 2018.12.04 |