Android編程出現(xiàn)Button點擊事件無效的解決方法示例
本文實例講述了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)聽按鈕了。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲的相關(guān)資料,數(shù)據(jù)存儲是Android開發(fā)的重要的知識,這里提供了實例,需要的朋友可以參考下2017-07-07Android通過手機拍照或從本地相冊選取圖片設(shè)置頭像
微信、QQ、微博等社交類APP如何更換自己的頭像,這篇文章主要介紹了Android通過手機拍照或從本地相冊選取圖片設(shè)置頭像,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android開發(fā)中的幾種網(wǎng)絡(luò)請求方式詳解
本篇文章主要包括Android中的幾種網(wǎng)絡(luò)請求方式詳解,具有一定的參考價值,有需要的可以了解一下。2016-11-11Android自定義ViewGroup打造各種風(fēng)格的SlidingMenu
這篇文章主要介紹了Android自定義ViewGroup打造各種風(fēng)格的SlidingMenu的相關(guān)資料,需要的朋友可以參考下2016-02-02