android:onClick attribute
1373 ワード
ここではButtonを例に紹介します
1』XMLファイルコードは以下の通りです.
The
2》Open the
注意:
In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically,the method must:(この方法はすべて以下の3つの条件を満たさなければならない)•Be public.(public) •Have a void return value.(戻り値はvoid)•Have a Viewas the only parameter(this will be the View that was clicked).(ps:ビューのパラメータタイプが1つしかなく、特に重要です.そうしないと、ボタンをクリックしたときにメソッドが呼び出されません.)
これもandroid:onClickプロパティが指定されており、Activityでも対応するメソッドが実装されている場合がありますが、実際に実行する場合は指定されたメソッドが実行されていない理由です.あなたの方法が上記の3つの条件を同時に満たしているかどうかをよく見てみましょう!!
1』XMLファイルコードは以下の通りです.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
The
android:onClick
attribute’s value, "sendMessage"
, is the name of a method in your activity that the system calls when the user clicks the button. 2》Open the
Activity
class (located in the project's src/
directory) and add the corresponding method: /** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
注意:
In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically,the method must:(この方法はすべて以下の3つの条件を満たさなければならない)•Be public.(public) •Have a void return value.(戻り値はvoid)•Have a Viewas the only parameter(this will be the View that was clicked).(ps:ビューのパラメータタイプが1つしかなく、特に重要です.そうしないと、ボタンをクリックしたときにメソッドが呼び出されません.)
これもandroid:onClickプロパティが指定されており、Activityでも対応するメソッドが実装されている場合がありますが、実際に実行する場合は指定されたメソッドが実行されていない理由です.あなたの方法が上記の3つの条件を同時に満たしているかどうかをよく見てみましょう!!