(转)Android——设置监听器的四种方式

Android四种监听方式:

  • 实现监听的接口
  • 实现匿名内部类
  • 使用外部类
  • 直接在xml中设置监听

1、使用接口方式实现监听事件:

可直接在Activity中定义事件处理方法

优点:简洁

缺点:可能造成程序结构混乱

2、实现匿名内部类实现监听:

优点:可以在当前类中复用该监听器,可自由访问外部类的所有界面组件

3、使用外部类实现监听:

优点:当某事件监听器被多个GUI界面共享,且只要是完成某种业务逻辑的实现

缺点:不利于提供程序内聚性,不能自由访问创建GUI界面类的组件,界面不够简洁

4、直接在xml文件中设置监听:

在需要监听的控件中添加:Android:onClick="xxx"

再在布局对应的Activity中定义public void xxx(View view){}


测试程序:

activity_main:


  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app= "http://schemas.android.com/apk/res-auto"
  4. xmlns:tools= "http://schemas.android.com/tools"
  5. android:layout_width= "match_parent"
  6. android:layout_height= "match_parent"
  7. android:orientation= "vertical"
  8. tools:context= ".MainActivity">
  9. <Button
  10. android:id= "@+id/button1"
  11. android:text= "接口方式设置监听器"
  12. android:layout_width= "match_parent"
  13. android:layout_height= "wrap_content" />
  14. <Button
  15. android:id= "@+id/button2"
  16. android:text= "直接在xml文件中绑定"
  17. android:layout_width= "match_parent"
  18. android:layout_height= "wrap_content"
  19. android:onClick= "button2OnClick"/>
  20. <Button
  21. android:id= "@+id/button3"
  22. android:text= "使用匿名内部类设置监听器"
  23. android:layout_width= "match_parent"
  24. android:layout_height= "wrap_content" />
  25. <Button
  26. android:id= "@+id/button4"
  27. android:text= "使用外部类设置监听器"
  28. android:layout_width= "match_parent"
  29. android:layout_height= "wrap_content" />
  30. </LinearLayout>

MainActivity:


  
  
  1. import android.content.Context;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.Toast;
  7. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  8. private Context context = MainActivity. this;
  9. private Button button1;
  10. private Button button2;
  11. private Button button3;
  12. private Button button4;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. button1 = (Button) findViewById(R.id.button1);
  18. button2 = (Button) findViewById(R.id.button2);
  19. button3 = (Button) findViewById(R.id.button3);
  20. button4 = (Button) findViewById(R.id.button4);
  21. button1.setOnClickListener( this);
  22. button3.setOnClickListener( new View.OnClickListener() {
  23. @Override
  24. public void onClick(View view) {
  25. Toast.makeText(context, "使用匿名内部类实现监听", Toast.LENGTH_SHORT).show();
  26. }
  27. });
  28. button4.setOnClickListener( new MyOnClickListener() {
  29. public void onClick(View view){
  30. super.onClick(view);
  31. }
  32. });
  33. }
  34. public void button2OnClick(View view){
  35. Toast.makeText(context, "直接在xml文件中绑定", Toast.LENGTH_SHORT).show();
  36. }
  37. @Override
  38. public void onClick(View view) {
  39. switch (view.getId()){
  40. case R.id.button1:
  41. Toast.makeText(context, "以接口的方式设置监听器", Toast.LENGTH_SHORT).show();
  42. break;
  43. }
  44. }
  45. class MyOnClickListener implements View.OnClickListener{
  46. @Override
  47. public void onClick(View view) {
  48. Toast.makeText(context, "使用外部类设置监听器", Toast.LENGTH_SHORT).show();
  49. }
  50. }
  51. }


发布了71 篇原创文章 · 获赞 130 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_40563761/article/details/104867372