android:onClick attribute
本文以Button為例進行介紹
1》XML文件代碼如下:
<Buttonandroid: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'ssrc/ 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:(該方法必須全部滿足以下三個條件:)
?Be public.(public)
?Have a void return value.(返回值為void)
?Have a View as the only parameter (this will be the View that was clicked).(ps:有且僅有一個參數類型為View的參數,這一點特別重要,否則點擊該按鈕時,不會調用該方法。)
這也是有時候明明指定了android:onClick屬性,并且Activity中也實現了對應的方法,但是實際執行的時候就是沒有執行指定的方法的原因。仔細看一下你的方法是否同時滿足以上三個條件!!!
總結
以上是生活随笔為你收集整理的android:onClick attribute的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下对Verilog文件进行le
- 下一篇: android:parentActivi