搬运自本人博客,xge技术博客
http://www.xgezhang.com/android_button_onclick_4_ways.html
Android下,按钮点击事件是在开发过程中经常会写到的东西。这里总结一下常见的四种写法:
界面代码就是一个button控件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = "com.xgezhang.setonclick4ways.MainActivity" > < Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Button" /> </ RelativeLayout > |
1.通过匿名内部类实现监听类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.button1); bt.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(MainActivity. this , "way1" , Toast.LENGTH_SHORT); tst.show(); } }); } } |
2.自定义一个个内部类,实现接口函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.button1); bt.setOnClickListener( new MyOnClickListener()); } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(MainActivity. this , "way2" , Toast.LENGTH_SHORT); tst.show(); } } } |
上述两种方法麻烦之初在于,如果有很多的Button,那么就需要对每一个Button都写一个类,代码比较复杂。
3.让MainActivity实现接口函数,通过switch语句判断R.id,来对不同的Button按钮做出不同的响应。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.button1); bt.setOnClickListener( this ); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: Toast tst = Toast.makeText(MainActivity. this , "way3" , Toast.LENGTH_SHORT); tst.show(); break ; default : break ; } } } |
4.在XML文件中“显式指定按钮的onClick属性,点击按钮时会利用反射的方式调用对应Activity中的click()方法:
XML文件添加一行:
1 | android:onClick="onClick" |
对应Java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText( this , "way4" , Toast.LENGTH_SHORT); tst.show(); } } |
当然,第四种方法也可以通过switch语句判断哪一个Button被点击。
在实际开发过程中,使用最多的是第三种方法。