[Android开发]android-按钮事件

Android SDK 2.0之后的版本提供了更简洁的方法,在Activity里创建一个public方法(记得设置View参数),然后在 Layout方法里直接设置。
逻辑和设计相比上边的简写和findViewById方法都更简单明了,更具可读性,也方便以后维护。

 

方式一:在配置中定义不同onClick事件方法

 

Activity中代码:

Java代码 复制代码  收藏代码
  1. public void myClickButton1(View v){   
  2.     Toast.makeText(this"Toast:Button_Test1", Toast.LENGTH_SHORT).show();   
  3. }   
  4.   
  5. public void myClickButton2(View v){   
  6.     Toast.makeText(this"Toast:Button_Test2", Toast.LENGTH_SHORT).show();   
  7. }  
    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中配置内容如下:

Xml代码 复制代码  收藏代码
  1. <Button     
  2.     android:id="@+id/idButtonTest1"  
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="wrap_content"    
  5.     android:text="@string/button_test1"  
  6.     android:onClick="myClickButton1"  
  7.     />  
  8. <Button     
  9.     android:id="@+id/idButtonTest2"  
  10.     android:layout_width="fill_parent"    
  11.     android:layout_height="wrap_content"    
  12.     android:text="@string/button_test2"  
  13.     android:onClick="myClickButton2"  
  14.     />  

 

注意:main.xml中的配置android:onClick="myClickButton1"和android:onClick="myClickButton2",如果熟悉JavaScript的朋友就会发现,这和JavaScript的onclick事件定义方法一模一样啊! :-)

 

 

方式二:在配置中定义相同的onClick事件方法,然后在onClick方法中根据不同的按钮执行不同的操作。

 

Activity中代码:

 

Java代码 复制代码  收藏代码
  1. public void myClickHandler(View v){   
  2.     int id = v.getId();   
  3.     switch(id){   
  4.         case R.id.idButtonTest1:   
  5.             Toast.makeText(demo2.this"Toast1:Button_Test1", Toast.LENGTH_SHORT).show();   
  6.             break;   
  7.         case R.id.idButtonTest2:   
  8.             Toast.makeText(demo2.this"Toast2:Button_Test2", Toast.LENGTH_SHORT).show();   
  9.             break;   
  10.         default:   
  11.             Toast.makeText(demo2.this"Toast:none", Toast.LENGTH_SHORT).show();   
  12.     }   
  13. }  
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配置内容如下:

Xml代码 复制代码  收藏代码
  1. <Button     
  2.     android:id="@+id/idButtonTest1"  
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="wrap_content"    
  5.     android:text="@string/button_test1"  
  6.     android:onClick="myClickHandler"  
  7.     />  
  8. <Button     
  9.     android:id="@+id/idButtonTest2"  
  10.     android:layout_width="fill_parent"    
  11.     android:layout_height="wrap_content"    
  12.     android:text="@string/button_test2"  
  13.     android:onClick="myClickHandler"  
  14.     />  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值