Focus issues in Android: focusable, clickable, and enabled

Source: Internet
Author: User

Focus issues in Android: focusable, clickable, and enabled

First, I excerpted an inspired answer from stackoverflow:

Try by Changing your code:


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


That is to say, when the clearfocus of EditText clears the focus, the framework tends to focus on the first view on the attempt layer that can obtain the focus.

In fact, the View attributes have different configurations corresponding to different events.

 

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

Familiar with these things, it is often used to configure a background selector in the drawable file. in xml, you can configure the background displayed when the focus is obtained, the background pressed down, and the background set to "enabled" to "false.

 

However, in a development project, I encounter the following scenarios:

 

<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 = save android: textColor = @ color/common_white android: textSize = 17sp/>
    
     
      
       
        
         
          
            
             
            
           
          
                 
        
       
      
         
    

The above is my layout, which is simple for user editing. The textview saved in the header is set to android: focusable = true.
Android: focusableInTouchMode = true. When you press edittext to obtain the focus, the soft keyboard pops up. When you edit and press save, The onclick method in setonclicklistener of the textview is not called.

 

I keep wondering why, and then I can see that "when the clearfocus of EditText clears the focus, the framework tends to focus on the first view on the attempt layer that can get the Focus, in fact, it is the view that can get the focus.

The textview saved in the header is not on the top. I personally think it is not. Because when I set the focus attribute of the header to false, I can click it. (PS: Default android: focusable = false android: focusableInTouchMode = false ).

Here, my inference is: After you configure the focusable attribute for the view, you also set the onclicklistener. If other edittexts obtain the focus, when you click the view that can be focusable, you will find that the onClick method of the view cannot get the callback. That is to say, focus and setOnClick are two conflicting operations. By default, if this view has been focused in the android system, onClick should not be executed.

 

How many guesses can be made from the system source code?

 

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;    }
     
    

This is an internal class in the class View. It is observed that there are clicklistener and focuschangelistener among others, so do not mix focus and click into a beach.

 

 

</Framelayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.