本文實例講述了Android編程出現(xiàn)Button點擊事件無效的解決方法。分享給大家供大家參考,具體如下:
遇到這樣一個問題,給一個界面上方的按鈕添加了點擊事件,但死活沒反應(yīng),而放在界面下方的3個按鈕,都有相應(yīng)點擊事件,百度了一下無非有兩種可能:
1.button沒有初始化或者button初始化多次,導(dǎo)致混亂。
2.button點擊事件寫錯,無法監(jiān)聽。
但我確定的是這些都是沒有錯的,后來找到的原因是下方的scroll布局覆蓋了上方的button的布局,使用了fill_parent,所以獲取不到點擊事件,如下出錯代碼:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="<span style="font-family: Arial, Helvetica, sans-serif;">fill_parent</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span> android:orientation="horizontal" > <Button android:id="@+id/canshusback" android:layout_width="25dp" android:layout_height="25dp" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/last" /> </LinearLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/allti" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" >
后來將上方的按鈕設(shè)置成高度40dp之后,將下方的scroll離上方有一段距離,就可以監(jiān)聽按鈕了。
希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選