方式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");

}

更多相关文章

  1. android中禁止GridView上下滑动的方法
  2. Android自定义属性,attr format取值类型
  3. Android(安卓)Service用法总结和生命周期详解
  4. Android输入法之如何自定义每个key的属性
  5. Android(Java):HTTP协议
  6. android控件之spinner (下拉列表)
  7. Android(安卓)3.0动画系统详解
  8. Android(安卓)NetworkImageView点击查看大图 自定义属性
  9. 设置屏幕默认横屏

随机推荐

  1. Unity出现 error building player except
  2. 如何掌握Android-Camera模块
  3. ListView中点击事件无法响应
  4. Android 模拟器上安装APK步骤
  5. android:configChanges的用法
  6. Android代码内存优化建议-OnTrimMemory优
  7. Android(安卓)Studio Error -- Could not
  8. android 开发过程中遇到问题
  9. TextView属性
  10. Android 2.1 Compatibility Definition