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