The focus issues involved in Android, focusable,clickable,enabled

Source: Internet
Author: User

First excerpt from the StackOverflow of the previous revelation of my answer:

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 the 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 was the first from the top so can take focus, then all callbacks related to clearing focus would be in Voked after wich the framework would give focus to this view.
means must set Focus to other view on button click because Text1 act as first view in your layout


This means that when EditText's Clearfocus clears the focus, the frame tends to focus on the first view that can be focused on the attempted layer.

In fact, the properties of the view are configured with different events.

Android:focusable= "false" Android:focusableintouchmode= "false" android:clickable= "true" android:enabled= "true" Android:state_enabled= "false" android:state_pressed= "true" android:state_focused= "true"

Very familiar with these things. The most common place to use is when configuring a background selector.xml under the drawable file to configure what background to display when the focus is given, what background to press down, and what background the enabled is false.

But in the development project. The scenario I encountered was:

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android : layout_width= "match_parent" android:layout_height= "match_parent" > <com.widget.titlebar android:id= "@+ Id/title_bar "android:layout_width=" Match_parent "android:layout_gravity=" Top "android:layout_height=            "49DP" > <textview android:id= "@+id/titlebar_save" android:layout_width= "60DP" android:layout_height= "Match_parent" android:layout_alignparentright= "true" 
<span style= "White-space:pre" ></span> 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 "/> </com.widget.TitleBar> <scrollview android:layout_width=" Match_parent "android:layout _height= "Match_parent" android:layout_margintop= "49DP" android:scrollbars= "None" > <linearlayou T android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orient ation= "vertical" > <relativelayout android:id= "@+id/account_head_layout" an Droid:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:paddingbottom = "25DP" Android:paddingleft= "12DP" android:paddingright= "12DP" android:paddingtop= "25DP" > & Lt                    TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centervertical= "true" android:text= "Avatar" Android:text                    Color= "@color/black_333333" android:textsize= "16sp"/> <imageview                    Android:id= "@+id/ico_set_arrow" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" Andro id:background= "@color/transparent" android:src= "@drawable/ico_set_arrow" Android:lay Out_centervertical= "true" android:layout_marginleft= "12DP"/> <com.widget.roundedim              Ageview      Android:id= "@+id/account_head" android:layout_width= "76DP" Android:layout_heig ht= "76DP" android:layout_centervertical= "true" android:layout_toleftof= "@id/ico_set_ Arrow "android:scaletype=" Centercrop "android:src=" @drawable/default_head "/> < /relativelayout> <include style= "@style/setting_dividing_line_style" an Droid:layout_width= "Match_parent" android:layout_height= "1.5DP" android:layout_marginleft= "1 2DP "android:layout_marginright=" 12DP "android:layout_margintop=" 10DP "layout = "@layout/widget_horizontal_dividing_line"/> <relativelayout Android Oid:layout_width= "Match_parent" android:layout_height= "50DP" android:paddingleft= "12 DP "Android:paddingright= "20DP" > <textview style= "@style/setting_item_text_sty                         Le "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "                        Android:layout_alignparentleft= "true" android:layout_centervertical= "true" android:text= "nickname"/> <linearlayout Android:layout_widt H= "0DP" android:layout_height= "0DP" android:focusable= "true" Android:focusableintouchmode = "true" > <requestfocus/> </LinearLayout> <com.widget.custominputbo                        X android:id= "@+id/account_nickname" android:layout_width= "120DP"                        android:layout_height= "Match_parent" android:singleline= "true" android:paddingtop= "5DP"                        Android:gravity= "Center_vertical" android:layout_alignparentright= "true" Android:layout_centervertical= "true" android:background= "@color/transparent"/ > </RelativeLayout> <include style= "@style/setting_dividing_l                    Ine_style "android:layout_width=" match_parent "android:layout_height=" 0.5DP " android:layout_marginleft= "12DP" android:layout_marginright= "12DP" layou t= "@layout/widget_horizontal_dividing_line"/> <relativelayout android:id= "@+id/ac                    Count_sex_layout "android:layout_width=" match_parent "android:layout_height=" 50DP " android:paddingleft= "12DP" android:paddingright= "20DP" > <te                  Xtview      style= "@style/setting_item_text_style" android:layout_width= "Wrap_content"                        android:layout_height= "Wrap_content" android:layout_alignparentleft= "true"                        Android:layout_centervertical= "true" android:text= "gender"/> <textview                        Android:id= "@+id/account_sex" style= "@style/account_item_text_style"                        Android:layout_width= "80DP" android:layout_height= "Match_parent"                        Android:gravity= "Center_vertical|right" android:layout_alignparentright= "true" Android:layout_centervertical= "true" android:text= "Confidential"/> </relativelayo                    ut> <include style= "@style/setting_dividing_line_style" Android:layout_Width= "Match_parent" android:layout_height= "0.5DP" android:layout_marginleft= "12DP" android:layout_marginright= "12DP" layout= "@layout/widget_horizontal_dividing_line"/ > <linearlayout android:layout_width= "match_parent" android:layout_height= "                Wrap_content "android:layout_margintop=" 40DP "android:orientation=" vertical "                    android:paddingright= "0DP" android:paddingbottom= "20DP" > <textview style= "@style/setting_item_text_style" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_marginleft= "12DP" android:text= "Personality signature"/&                Gt <include style= "@style/setting_dividing_line_style" android:layout_width= "match_p Arent"Android:layout_height=" 0.5DP "android:layout_marginleft=" 12DP " android:layout_marginright= "0DP" android:layout_margintop= "10DP" layout= "@layout/wid                    Get_horizontal_dividing_line "/> <edittext android:id=" @+id/account_description "                    style= "@style/account_item_text_style" android:hint= "Introduce yourself in a passage ..." android:gravity= "Top" android:layout_width= "Match_parent" android:layout_height= "Wra                    P_content "android:layout_marginleft=" 20DP "android:layout_marginright=" 20DP " android:layout_margintop= "10DP" android:paddingbottom= "10DP" Android:ba                    ckground= "@color/transparent" android:ellipsize= "End" android:maxlines= "3" Android:minlines= "3"/> </LinearLayout> </LinearLayout> </ScrollView></FrameLayout> 

Above is my layout, on a simpler user edit, the head of the TextView set android:focusable= "true"
Android:focusableintouchmode= "True" for both properties, resulting when the soft keyboard is ejected by pressing EditText to get focus, The editor does not call the OnClick method inside the TextView Setonclicklistener when saved.

Have always wanted to be unclear why, and then see " when EditText's Clearfocus clears the focus, the frameworkprefers to focus on the first point of view that can be focused on the attempt layer, which is actually the view that gets the focus."

The TextView of the head here is actually not on top, as the individual thinks not. Because when I set the focus two property of the head to False, I was able to click.

(PS: The default android:focusable= "false " android:focusableintouchmode= "false" these two properties are false).

My judgment here is: When you configure the focusable attribute for view, the same time is also setonclicklistener, assuming that the other edittext get the focus, Then when you click on a view that can be focusable, you find that the OnClick method of the view cannot get a callback. This means that focus and setonclick are actually two conflicting operations. The Android system defaults to assume that the view already has focus and should not run the onclick.


See how much the system source code can also be measured to

Static Class Listenerinfo {/** * Listener used to dispatch focus change events.         * This field should was made private, so it's hidden from the SDK.        * {@hide} */protected Onfocuschangelistener Monfocuschangelistener;         /** * Listeners for layout change events.        */Private arraylist<onlayoutchangelistener> monlayoutchangelisteners;         /** * Listeners for attach events.        */Private copyonwritearraylist<onattachstatechangelistener> monattachstatechangelisteners;         /** * Listener used to dispatch click events.         * This field should was made private, so it's hidden from the SDK.        * {@hide} */public onclicklistener Monclicklistener;         /** * Listener used to dispatch long click events.         * This field should was made private, so it's hidden from the SDK. * {@hide} */protected Onlongclicklistener MonloNgclicklistener;         /** * Listener used to build the context menu.         * This field should was made private, so it's 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 inner class in Class View. Observe that there are clicklistener,focuschangelistener and so on, so in fact don't mix focus and click into one beach.


PS: Suppose there are different views, welcome message exchange









The focus issues involved in Android, focusable,clickable,enabled

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.