1
如果我想单击按钮时发生某些事情,两种方法的区别是什么?第一个看起来简单得多。Android事件处理方法
在布局
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
在活动
public void sendMessage(View v) {
// do whatever
}
OR
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// do whatever
}
};
protected void onCreate(Bundle savedValues) {
// Capture our button from layout
Button button = (Button)findViewById(R.id.mybutton);
// Register the onClick listener with the implementation above
button.setOnClickListener(listener);
}
看看这个问题和答案:http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – jenzz