如何讓TextView後面的Layout響應Click事件?

有一個Layout(LinearLayout或者RelativeLayout)做爲背景,他有一個TextView做爲子元素,當點擊TextView控件時,想讓它所在的Layout響應。這個問題其實有兩個解決方法:android

一、在TextView中聲明spa

 <TextView
                android:id="@+id/my_recent_voice_edit"
                android:layout_width="148.3dp"
                android:layout_height="wrap_content"
                android:duplicateParentState="true"
                android:gravity="center"
                android:text="@string/voice_recent_btn_text"
                android:textColor="@color/update_voice_recent"
                android:color="#666666"
                android:layout_marginBottom="1dp"
                android:clickable="true"
                android:textSize="12sp" />3d

在它的父Layout中聲明clickable爲true。事件

二、屏蔽Layout的子元素獲取焦點。這種方法string

<LinearLayout
            android:id="@+id/voice_recent_edit_layout"
            android:layout_width="193.3dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="33dp"
            android:layout_marginRight="33dp"
            android:background="@drawable/recent_voice_bg"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:gravity="center_vertical"
            android:visibility="gone" >it

這兩種方法雖然均可以達到一樣的效果,但他們仍是有區別的。其中最重要的一點就是後一種方法中不能爲TextView指定selector了;還有一個比較奇怪的區別就是:使用第一種方法判斷長按事件有時候會失效,而第二種方法判斷長按事件效果很好。cli

相關文章
相關標籤/搜索