先说区别:
两种方式底层一样,只是使用onClick需要注意:
1. 事件处理函数必须是public的。
2. 事件处理函数必须在Activity中定义。
3. 事件处理函数必须有一个View类型的参数。
4. 只能用在API Level 4及以后的版本。
1.onClick方法:在Xml中添加onClick属性
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Press Me"
android:onClick="buttonClick"/>
</RelativeLayout>
在代码中添加对应的函数buttonClick,注意要添加到当前Activity对应的java类