android响应事件(按钮)的三种方式
方式1 采用匿名内部类方法
button1.setOnClickListener(newView.OnClickListener()
{
@Override
publicvoidonClick(View arg0) {
System.out.println("button1 clicked");
}
});
方式2 采用activity实现OnClickListener接口
publicclass TaskActivity extends Activity implements ClickListener
{
publicvoidonClick(View arg0)
{
if(arg0==button1)
System.out.println("button1 clicked");
else if(arg0==button2)
System.out.println("button2 clicked");
...
}
}
方式3修改XML android:onClick 属性
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
publicvoidselfDestruct(View arg0) {
System.out.println("button1 clicked");
}
更多相关文章
- android中禁止GridView上下滑动的方法
- Android自定义属性,attr format取值类型
- Android(安卓)Service用法总结和生命周期详解
- Android输入法之如何自定义每个key的属性
- Android(Java):HTTP协议
- android控件之spinner (下拉列表)
- Android(安卓)3.0动画系统详解
- Android(安卓)NetworkImageView点击查看大图 自定义属性
- 设置屏幕默认横屏