onclicklistener到底怎么用?
2013-11-19 11:01
417 查看
转载地址:http://blog.csdn.net/dickren123/article/details/7216975
相信很多像我一样的新手学习ANDROID开发会遇到这个问题,通过这几天的归类和总结,将我的理解写在下面,欢迎大家一起前来讨论:
以按钮BUTTON的监听事件为例,以下的监听实现都是等价的:
1.使用接口继承按钮监听方法:
2.使用接口继承view类的监听方法
3.不用接口,在类内部直接实现监听
如果不使用匿名实例,也可以定义一个具体的实例,如下:
希望大家多多交流!^_^
相信很多像我一样的新手学习ANDROID开发会遇到这个问题,通过这几天的归类和总结,将我的理解写在下面,欢迎大家一起前来讨论:
以按钮BUTTON的监听事件为例,以下的监听实现都是等价的:
1.使用接口继承按钮监听方法:
package dickren123.hui.say_hello_to_world; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; /* 这里接口继承的方法是隶属于按钮BUTTON的,因此前面导入的头文件只需有BUTTON即可*/ public class Hello_to_worldActivity extends Activity implements Button.OnClickListener{ /** Called when the activity is first created. */ private Button btn_say_hello; private TextView hello_world; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD); hello_world = (TextView)findViewById(R.id.textView1); btn_say_hello.setOnClickListener(this) ;//由于该类继承了BUTTON的监听, } //因此设置监听的参数只需传本类的对象即可 public void onClick(View v) { // TODO Auto-generated method stub hello_world.setText("dickren123!");//抽象接口的内部方法的实现 } }
2.使用接口继承view类的监听方法
package dickren123.hui.say_hello_to_world; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;/* 导入的头文件需要有view类监听*/ import android.widget.Button; import android.widget.TextView; public class Hello_to_worldActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ private Button btn_say_hello; private TextView hello_world; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD); hello_world = (TextView)findViewById(R.id.textView1); btn_say_hello.setOnClickListener(this) ;//由于该类继承了view的监听, } //因此设置监听的参数只需传本类的对象即可 public void onClick(View v) { // TODO Auto-generated method stub hello_world.setText("dickren123!");//抽象接口的内部方法的实现 } }
3.不用接口,在类内部直接实现监听
package dickren123.hui.say_hello_to_world; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Hello_to_worldActivity extends Activity { /** Called when the activity is first created. */ private Button btn_say_hello; private TextView hello_world; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD); hello_world = (TextView)findViewById(R.id.textView1); btn_say_hello.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { //使用匿名的Button实例 // TODO Auto-generated method stub hello_world.setText("dickren123!");//抽象接口的内部方法的实现 } }) ; } }
如果不使用匿名实例,也可以定义一个具体的实例,如下:
package dickren123.hui.say_hello_to_world; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Hello_to_worldActivity extends Activity { /** Called when the activity is first created. */ private Button btn_say_hello; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD); btn_listener bl = new btn_listener(); btn_say_hello.setOnClickListener(bl); //bl是类btn_listener的实例,而btn_listener为监听方法的接口 } //因此设置监听的参数只需传本类的对象即可 } class btn_listener implements Button.OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub } }
希望大家多多交流!^_^
相关文章推荐
- Android开发——onclicklistener()到底怎么用?
- onclicklistener到底怎么用? .
- 车险到底怎么买最划算
- 微软到底怎么了?
- 单卡12.8TB闪存卡到底怎么用?
- 到底怎么理解这个复数和复指数??
- Hadoop到底能做什么?怎么用hadoop?
- “外部”和“内部”到底是怎么一回事呢?
- 学java到底该要怎么学!!!!
- 公司到底是怎么看我们的……
- SUN到底是怎么想的?一个空格给我过了个愚人节!
- 到底怎么查数据库字符集
- QQ木马到底怎么做?QQ挂马方法大揭密(图)
- 疯狂暴涨曾破 2 万美元的比特币,到底是怎么一回事?
- Hadoop到底能做什么?怎么用hadoop?
- 十连跌!这个一线城市的房价到底怎么了?
- 关于战斗力计算模型的补充——到底怎么配点属性才会最强呢?
- 程序员到底怎么了
- 到底怎么了?
- 当网络管理员遭遇网络不通到底该怎么办