關於EditText的OnClickListener失效的解決辦法


最近開發,遇到一個問題,就是如果EditText只作為顯示,不需要編輯文本,但需要點擊該布局可以執行其他事件,就會沖突,EditText依然處於文本編輯狀態;

如:

如:有5個EditText,最后一個只需要顯示,不需要點擊編輯文本,只需要點擊該布局跳轉到日歷選擇界面:

思路如下:

監聽EditText的父布局LinearLayout,,但是EditText還是優先響應,就算設置了

edt_birthday.setClickable(false);
edt_birthday.setFocusable(false);

那么也就是沒響應而已;

 

繼續摸索:

突然想到可以有FrameLayout:

 1 <LinearLayout
 2             android:id="@+id/ll_birthday" 
 3             android:layout_width="match_parent"
 4             android:layout_height="60dp"
 5             android:background="@color/white"
 6             android:orientation="horizontal"
 7             android:gravity="center_vertical"
 8             >
 9             <TextView 
10                 android:layout_width="wrap_content"
11                 android:layout_height="wrap_content"
12                 style="@style/text_16_444444"
13                 android:text="生日"
14                 />
15             <FrameLayout 
16                 android:layout_width="match_parent"
17                 android:layout_height="fill_parent"
18                 >
19                 <EditText
20                     android:id="@+id/edt_birthday" 
21                     android:layout_width="match_parent"
22                     android:layout_height="wrap_content"
23                     style="@android:style/Widget.TextView"
24                     android:background="@null"
25                     android:hint="請選擇"
26                     android:textColor="@color/font_444444"
27                     android:textSize="16dp"
28                     android:layout_marginLeft="30dp"
29                     android:drawableRight="@drawable/icon_right"
30                     android:editable="false"
31                     android:maxLines="1"
32                     android:layout_gravity="center_vertical"
33                     />
34                 <View 
35                     android:id="@+id/view_birthday"
36                     android:layout_width="match_parent"
37                     android:layout_height="fill_parent"
38                     android:background="@color/background_transparent"
39                     />
40             </FrameLayout>
41             
42         </LinearLayout>

關鍵是EditText上面覆蓋一個透明的View,那么該View就會優先獲得點擊響應事件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM