android-学习笔记之按钮事件
本文转载自: http://zhangkun716717-126-com.iteye.com/blog/761080
前备知识:
1.需要了解怎么得到界面元素。
那么如何得到界面元素呢?在界面配置文件:例如 main.xml 中,比方一个id为idButtonTest1的Button定义如下:
- <Button
- android:id="@+id/idButtonTest1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test1"
- />
- <Button
- android:id="@+id/idButtonTest2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test2"
- />
在string常量配置文件string.xml中,配置如下常量
- <string name="button_test1">测试按钮1</string>
- <string name="button_test2">测试按钮2</string>
那么得到该Button的做法就是findViewById(R.id.idButtonTest1);
比如:
- Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);
buttonTest1就是那个id为idButtonTest1的Button了。
好了,下边开始今天的主题。今天讲的主要是OnClickListener,该类位置:import android.view.View.OnClickListener;
那么如何给刚才那个Button添加click事件呢?代码如下:
- buttonTest1.setOnClickListener(newOnClickListener);
这里的newOnClickListener是一个OnClickListener对象:
- private OnClickListener newOnClickListener = new OnClickListener(){
- @Override
- public void onClick(View v) {
- Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
- }
- };
这是比较罗嗦,或者说是比较婆妈的做法,实际使用中我们可以简写为:
- findViewById(R.id.idButtonTest1).setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
- }
- });
不过不推荐直接用findViewById(R.id.idButtonTest1),不规范呀么不规范~
还有一种则是把全部的click事件给一个clickHandler来处理:
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);
- Button buttonTest2 = (Button)findViewById(R.id.idButtonTest2);
- buttonTest1.setOnClickListener(clickHandler);
- buttonTest2.setOnClickListener(clickHandler);
- }
- private OnClickListener clickHandler = new OnClickListener(){
- @Override
- public void onClick(View v) {
- int id = v.getId();
- switch(id){
- case R.id.idButtonTest1:
- Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
- break;
- case R.id.idButtonTest2:
- Toast.makeText(demo2.this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();
- break;
- default:
- Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();
- }
- }
- };
Android SDK 2.0之后的版本提供了更简洁的方法,在Activity里创建一个public方法(记得设置View参数),然后在 Layout方法里直接设置。
逻辑和设计相比上边的简写和findViewById方法都更简单明了,更具可读性,也方便以后维护。
方式一:在配置中定义不同onClick事件方法
Activity中代码:
- public void myClickButton1(View v){
- Toast.makeText(this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();
- }
- public void myClickButton2(View v){
- Toast.makeText(this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();
- }
在main.xml中配置内容如下:
- <Button
- android:id="@+id/idButtonTest1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test1"
- android:onClick="myClickButton1"
- />
- <Button
- android:id="@+id/idButtonTest2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test2"
- android:onClick="myClickButton2"
- />
注意:main.xml中的配置android:onClick="myClickButton1"和android:onClick="myClickButton2",如果熟悉JavaScript的朋友就会发现,这和JavaScript的onclick事件定义方法一模一样啊! :-)
方式二:在配置中定义相同的onClick事件方法,然后在onClick方法中根据不同的按钮执行不同的操作。
Activity中代码:
- public void myClickHandler(View v){
- int id = v.getId();
- switch(id){
- case R.id.idButtonTest1:
- Toast.makeText(demo2.this, "Toast1:Button_Test1", Toast.LENGTH_SHORT).show();
- break;
- case R.id.idButtonTest2:
- Toast.makeText(demo2.this, "Toast2:Button_Test2", Toast.LENGTH_SHORT).show();
- break;
- default:
- Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();
- }
- }
main.xml配置内容如下:
- <Button
- android:id="@+id/idButtonTest1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test1"
- android:onClick="myClickHandler"
- />
- <Button
- android:id="@+id/idButtonTest2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/button_test2"
- android:onClick="myClickHandler"
- />