• onclicklistener到底怎么用?


    转载地址:http://blog.csdn.net/dickren123/article/details/7216975

    相信很多像我一样的新手学习Android开发会遇到这个问题,通过这几天的归类和总结,将我的理解写在下面,欢迎大家一起前来讨论:

    以按钮BUTTON的监听事件为例,以下的监听实现都是等价的:

    1.使用接口继承按钮监听方法

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.Button;
     5 import android.widget.TextView;
     6 
     7 /* 这里接口继承的方法是隶属于按钮BUTTON的,因此前面导入的头文件只需有BUTTON即可 */
     8 public class Hello_to_worldActivity extends Activity implements Button.OnClickListener {
     9     /** Called when the activity is first created. */
    10     private Button btn_say_hello;
    11     private TextView hello_world;
    12 
    13     @Override
    14     public void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.main);
    17         btn_say_hello = (Button) findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
    18         hello_world = (TextView) findViewById(R.id.textView1);
    19         btn_say_hello.setOnClickListener(this);// 由于该类继承了BUTTON的监听,
    20     } // 因此设置监听的参数只需传本类的对象即可
    21 
    22     public void onClick(View v) {
    23         // TODO Auto-generated method stub
    24         hello_world.setText("dickren123!");// 抽象接口的内部方法的实现
    25     }
    26 }

    2.使用接口继承view类的监听方法

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.view.View.OnClickListener;/* 导入的头文件需要有view类监听 */
     5 import android.widget.Button;
     6 import android.widget.TextView;
     7 
     8 public class Hello_to_worldActivity extends Activity implements OnClickListener {
     9     /** Called when the activity is first created. */
    10     private Button btn_say_hello;
    11     private TextView hello_world;
    12 
    13     @Override
    14     public void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.main);
    17         btn_say_hello = (Button) findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
    18         hello_world = (TextView) findViewById(R.id.textView1);
    19         btn_say_hello.setOnClickListener(this);// 由于该类继承了view的监听,
    20     } // 因此设置监听的参数只需传本类的对象即可
    21 
    22     public void onClick(View v) {
    23         // TODO Auto-generated method stub
    24         hello_world.setText("dickren123!");// 抽象接口的内部方法的实现
    25     }
    26 }  

    3.不用接口,在类内部直接实现监听

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.Button;
     5 import android.widget.TextView;
     6 
     7 public class Hello_to_worldActivity extends Activity {
     8     /** Called when the activity is first created. */
     9     private Button btn_say_hello;
    10     private TextView hello_world;
    11 
    12     @Override
    13     public void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.main);
    16         btn_say_hello = (Button) findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
    17         hello_world = (TextView) findViewById(R.id.textView1);
    18         btn_say_hello.setOnClickListener(new Button.OnClickListener() {
    19             public void onClick(View v) { // 使用匿名的Button实例
    20                 // TODO Auto-generated method stub
    21                 hello_world.setText("dickren123!");// 抽象接口的内部方法的实现
    22             }
    23         });
    24     }
    25 }

    4、如果不使用匿名实例,也可以定义一个具体的实例

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.View;
     4 import android.widget.Button;
     5 
     6 public class Hello_to_worldActivity extends Activity {
     7     /** Called when the activity is first created. */
     8     private Button btn_say_hello;
     9 
    10     @Override
    11     public void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.main);
    14         btn_say_hello = (Button) findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
    15         btn_listener bl = new btn_listener();
    16         btn_say_hello.setOnClickListener(bl); // bl是类btn_listener的实例,而btn_listener为监听方法的接口
    17     } // 因此设置监听的参数只需传本类的对象即可
    18 }
    19 
    20 class btn_listener implements Button.OnClickListener {
    21     public void onClick(View v) {
    22         // TODO Auto-generated method stub
    23 
    24     }
    25 }
  • 相关阅读:
    201871010114李岩松《面向对象程序设计(java)》第十二周学习总结
    201871010114李岩松《面向对象程序设计(java)》第十六周学习总结
    201871010114李岩松《面向对象程序设计(java)》第八周学习总结
    201871010114李岩松《面向对象程序设计(java)》第七周学习总结
    201871010114李岩松《面向对象程序设计(java)》第六、七周学习总结
    一分钟秒解数据库三大范式
    反射机制获取类的方法
    hashMap和hashTable的区别
    ssm+maven 框架整合
    Hibernate从零开始的反向工程
  • 原文地址:https://www.cnblogs.com/mukekeheart/p/5674664.html
Copyright © 2020-2023  润新知