您的位置:首页 > 移动开发 > Android开发

android响应事件(按钮)的三种方式

2013-06-05 17:06 537 查看
方式1 采用匿名内部类方法

button1.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View arg0) {

System.out.println("button1 clicked");

}

});

方式2 采用activity实现OnClickListener接口

public class TaskActivity extends Activity implements ClickListener

{

public void onClick(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:

public void selfDestruct(View arg0) {

System.out.println("button1 clicked");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: