android 控件监听的4种用法
Java代码
package com.cnd.zhongkong.qiyeban;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
/**
* button监听器用法,参考:https://blog.csdn.net/kyi_zhu123/article/details/52601691
*/
public class ActivityTest extends AppCompatActivity implements View.OnClickListener {
private static final String TAG="ActivityTest";
private Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btn1=(Button)findViewById(R.id.btnShow1);
btn2=(Button)findViewById(R.id.btnShow2);
btn3=(Button)findViewById(R.id.btnShow3);
/*********
* 方法一,匿名内部类监听优点有:
* 1.可以在当前类中复用该监听器类
* 2.可以自由访问外部类的所有界面组件
* 确定:如有多个控件一个个写很麻烦
* *****/
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG,"function1 click "+((Button)view).getText());
}
});
/*方法2:外部类实现监听
* 优点为:
1.当某个事件监听器被多个GUI界面共享,且主要是完成某种业务逻辑的实现
其中缺点为:
1.不利于提高程序的内聚性
2.不能自由访问创建GUI界面类的组件,编程不够简洁
* */
btn2.setOnClickListener(new OnClickListenerClass());
/*****
* 方法3:接口方式实现监听其中优点为:非常简洁
* 缺点为:
* 1.这种形式可能造成程序结构混乱。Activity的主要职责应该是完成界面初始化;但此时还需包含事件处理器方法,从而引起混乱
* 2.如果activity界面类需要实现监听器接口,让人感觉比较怪异
* ***/
btn3.setOnClickListener(this);
}//end onclick
@Override
public void onClick(View view) {
Log.i(TAG,"function3 click: "+((Button)view).getText());
}
public void clickHandle(View view){ /*方法4:直接绑定到标签,优点:某个控件的点击方法要实现的逻辑较复杂的时候使用,缺点:要在xml中写Android:onClick="clickHandler"*/
Log.i(TAG,"function4 click: "+((Button)view).getText());
}
class OnClickListenerClass implements View.OnClickListener{
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnShow2:Log.i(TAG,"function2 click "+((Button)view).getText());break;
}
}
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ActivityTest">
<Button
android:id="@+id/btnShow1"
android:layout_width="241dp"
android:layout_height="0dp"
android:layout_marginTop="250dp"
android:layout_marginBottom="135dp"
android:text="Button1"
app:layout_constraintBottom_toTopOf="@+id/btnShow2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnShow2"
android:layout_width="241dp"
android:layout_height="0dp"
android:layout_marginBottom="143dp"
android:text="Button2"
app:layout_constraintBottom_toTopOf="@+id/btnShow3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnShow1" />
<Button
android:id="@+id/btnShow3"
android:layout_width="241dp"
android:layout_height="0dp"
android:layout_marginBottom="91dp"
android:text="Button3"
app:layout_constraintBottom_toTopOf="@+id/btnShow4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnShow2" />
<Button
android:onClick="clickHandle"
android:id="@+id/btnShow4"
android:layout_width="241dp"
android:layout_height="0dp"
android:layout_marginBottom="181dp"
android:text="Button4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnShow3" />
</androidx.constraintlayout.widget.ConstraintLayout>