日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

android:onClick attribute

發(fā)布時(shí)間:2024/4/15 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android:onClick attribute 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文以Button為例進(jìn)行介紹

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:(該方法必須全部滿(mǎn)足以下三個(gè)條件:)
?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:有且僅有一個(gè)參數(shù)類(lèi)型為View的參數(shù),這一點(diǎn)特別重要,否則點(diǎn)擊該按鈕時(shí),不會(huì)調(diào)用該方法。)


這也是有時(shí)候明明指定了android:onClick屬性,并且Activity中也實(shí)現(xiàn)了對(duì)應(yīng)的方法,但是實(shí)際執(zhí)行的時(shí)候就是沒(méi)有執(zhí)行指定的方法的原因。仔細(xì)看一下你的方法是否同時(shí)滿(mǎn)足以上三個(gè)條件!!!




總結(jié)

以上是生活随笔為你收集整理的android:onClick attribute的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。