Android中涉及到的焦點問題,focusable,clickable,enabled

來源:互聯網
上載者:User

Android中涉及到的焦點問題,focusable,clickable,enabled

先摘抄下stackoverflow上一個啟發了我的回答:

try by Changing your code as:


private OnClickListener saveButtonListener = new OnClickListener() {


@Override
public void onClick(View v) {
Text1.clearFocus();
Text2.clearFocus();
saveButton.requestFocus(); //or any other View
}


}
because as doc as about public void clearFocus () :


Called when this view wants to give up focus. If focus is cleared onFocusChanged(boolean, int, android.graphics.Rect) is called.


Note: When a View clears focus the framework is trying to give focus to the first focusable View from the top. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after wich the framework will give focus to this view.
means you must set Focus to other view on button click because Text1 act as first View in your layout


意思就是說,當EditText的clearfocus清除焦點的時候,架構更傾向於把焦點給試圖層上的第一個能夠擷取焦點的view

其實View的屬性裡有一下配置分別對應不同的事件

 

android:focusable=falseandroid:focusableInTouchMode=falseandroid:clickable=trueandroid:enabled=trueandroid:state_enabled=falseandroid:state_pressed=trueandroid:state_focused=true

很熟悉這些東西,比較常用到的地方是在drawable檔案下配置一個背景的selector.xml的時候有配置當獲得焦點時顯示什麼背景,按下去什麼背景,enabled為false什麼背景。

 

但是開發項目中,我遇到的情境是:

 

<framelayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">            
    android:focusable=true        android:focusableInTouchMode=true            android:background=@drawable/titlebar_btn_bg_selector            android:gravity=center            android:text=儲存            android:textColor=@color/common_white            android:textSize=17sp />                                                 

上面是我的layout,就一個比較簡單的使用者編輯,頭部儲存的textview設定了android:focusable=true
android:focusableInTouchMode=true這兩個屬性,導致當按edittext擷取焦點彈出軟鍵盤後,編輯按儲存的時候沒有調用這個textview的setonclicklistener裡面的onclick方法。

 

一直想不明白為什麼,然後看到“當EditText的clearfocus清除焦點的時候,架構更傾向於把焦點給試圖層上的第一個能夠擷取焦點的view”這句話,其實是那些能夠擷取焦點的view。

這裡的頭部儲存的textview其實不是在top上的,個人覺得不是。因為當我把頭部的focus兩個屬性設為false後是可以點擊到的。(PS:預設的android:focusable=false android:focusableInTouchMode=false這兩個屬性是為false)。

這裡我的推斷是:當你為view配置了focusable屬性後,同時也setOnclickListener了,如果其他的EditText擷取焦點後,然後你點擊能夠focusable的view的時候發現這個view的onClick方法無法得到回調。也就是說focus和setOnClick其實是兩個互相矛盾的操作,android系統預設如果這個view已經focus獲得焦點了,就不應該執行onClick。

 

看系統源碼也能多少猜測到

 

static class ListenerInfo {        /**         * Listener used to dispatch focus change events.         * This field should be made private, so it is hidden from the SDK.         * {@hide}         */        protected OnFocusChangeListener mOnFocusChangeListener;        /**         * Listeners for layout change events.         */        private ArrayList mOnLayoutChangeListeners;        /**         * Listeners for attach events.         */        private CopyOnWriteArrayList mOnAttachStateChangeListeners;        /**         * Listener used to dispatch click events.         * This field should be made private, so it is hidden from the SDK.         * {@hide}         */        public OnClickListener mOnClickListener;        /**         * Listener used to dispatch long click events.         * This field should be made private, so it is hidden from the SDK.         * {@hide}         */        protected OnLongClickListener mOnLongClickListener;        /**         * Listener used to build the context menu.         * This field should be made private, so it is hidden from the SDK.         * {@hide}         */        protected OnCreateContextMenuListener mOnCreateContextMenuListener;        private OnKeyListener mOnKeyListener;        private OnTouchListener mOnTouchListener;        private OnHoverListener mOnHoverListener;        private OnGenericMotionListener mOnGenericMotionListener;        private OnDragListener mOnDragListener;        private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;        OnApplyWindowInsetsListener mOnApplyWindowInsetsListener;    }

這個是class View裡面的一個內部類,觀察發現有clicklistener,focuschangelistener等,所以其實不要把focus和click混為一灘。

 

 

</framelayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.