在看API Demo中的GestureBuilderActivity时候遇到了一个比较恶心的问题:
本来想看Add gesture这个Button的点击事件是如何写的,但是根据他的id没有能找到
<Button
android:id="@+id/addButton"
android:onClick="addGesture"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:text="@string/button_add" />
但是根据代码中函数的名字和断点调试发现走的就是下面的函数:
@SuppressWarnings({"UnusedDeclaration"})
public void addGesture(View v) {
Intent intent = new Intent(this, CreateGestureActivity.class);
startActivityForResult(intent, REQUEST_NEW_GESTURE);
}
仔细一看原来定义这个Button的时候有一个属性android:onClick="addGesture"
However, instead of applying an OnClickListener
to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick
attribute. For example:
<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 view) { // Kabloey }
The View
passed into the method is a reference to the widget that was clicked.