现在的位置: 首页 > 综合 > 正文

OnClickListener的不同用法

2019年07月29日 ⁄ 综合 ⁄ 共 4562字 ⁄ 字号 评论关闭

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

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

1.使用接口继承Button监听方法:

[javascript] view
plain
copy

  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. /* 这里接口继承的方法是隶属于按钮BUTTON的,因此前面导入的头文件只需有BUTTON即可*/  
  9. public class Hello_to_worldActivity extends Activity implements Button.OnClickListener{  
  10. /** Called when the activity is first created. */  
  11. private Button btn_say_hello;  
  12. private TextView hello_world;  
  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. public void onClick(View v) {  
  22. // TODO Auto-generated method stub  
  23. hello_world.setText("dickren123!");//抽象接口的内部方法的实现  
  24. }  
  25. }  



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

[javascript] view
plain
copy

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



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

[javascript] view
plain
copy

  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.   
  9. public class Hello_to_worldActivity extends Activity {  
  10. /** Called when the activity is first created. */  
  11. private Button btn_say_hello;  
  12. private TextView hello_world;  
  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(new Button.OnClickListener(){  
  20. public void onClick(View v) { //使用匿名的Button实例  
  21. // TODO Auto-generated method stub  
  22. hello_world.setText("dickren123!");//抽象接口的内部方法的实现  
  23. }  
  24. }) ;  
  25. }   
  26.  

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

[javascript] view
plain
copy

  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7.   
  8. public class Hello_to_worldActivity extends Activity {  
  9. /** Called when the activity is first created. */  
  10. private Button btn_say_hello;  
  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. btn_listener bl = new btn_listener();  
  18. btn_say_hello.setOnClickListener(bl); //bl是类btn_listener的实例,而btn_listener为监听方法的接口  
  19. //因此设置监听的参数只需传本类的对象即可  
  20. }  
  21. class btn_listener implements Button.OnClickListener  
  22. {  
  23. public void onClick(View v) {  
  24. // TODO Auto-generated method stub  
  25.   
  26. }  
  27. }  

4、一个类中,OnClickListener()继承不同的类,使用方法:

//显示带确定、取消、中立的对话框
class button1ClickListener implements OnClickListener{

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
		alert.setIcon(R.drawable.advise);
		alert.setTitle("系统提示");
		alert.setMessage("带确定、取消、中立的对话框");
		alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
				
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
					
			}
		});
	}
}

尤其注意:

class button1ClickListener implements OnClickListener

使用接口继承Button监听方法,继承  android.view.View.OnClickListener

alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener(

使用接口DialogInterface监听方法,继承 android.content.DialogInterface.OnClickListener

抱歉!评论已关闭.