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="false"android:focusableInTouchMode="false"android:clickable="true"android:enabled="true"android:state_enabled="false"android:state_pressed="true"android:state_focused="true"很熟悉这些东西,比较常用到的地方是在drawable文件下配置一个背景的selector.xml的时候有配置当获得焦点时显示什么背景,按下去什么背景,enabled为false什么背景。

但是开发项目中,我遇到的场景是:

<FrameLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent" ><com.widget.TitleBarandroid:id="@+id/title_bar"android:layout_width="match_parent"android:layout_gravity="top"android:layout_height="49dp" ><TextViewandroid: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="保存"android:textColor="@color/common_white"android:textSize="17sp" /></com.widget.TitleBar><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="49dp"android:scrollbars="none" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><RelativeLayoutandroid:id="@+id/account_head_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="25dp"android:paddingLeft="12dp"android:paddingRight="12dp"android:paddingTop="25dp" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="头像"android:textColor="@color/black_333333"android:textSize="16sp" /><ImageViewandroid:id="@+id/ico_set_arrow"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:background="@color/transparent"android:src="@drawable/ico_set_arrow"android:layout_centerVertical="true"android:layout_marginLeft="12dp"/><com.widget.RoundedImageViewandroid:id="@+id/account_head"android:layout_width="76dp"android:layout_height="76dp"android:layout_centerVertical="true"android:layout_toLeftOf="@id/ico_set_arrow"android:scaleType="centerCrop"android:src="@drawable/default_head" /></RelativeLayout><includestyle="@style/setting_dividing_line_style"android:layout_width="match_parent"android:layout_height="1.5dp"android:layout_marginLeft="12dp"android:layout_marginRight="12dp"android:layout_marginTop="10dp"layout="@layout/widget_horizontal_dividing_line" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:paddingLeft="12dp"android:paddingRight="20dp" ><TextViewstyle="@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="昵称" /><LinearLayout android:layout_width="0dp"android:layout_height="0dp"android:focusable="true"android:focusableInTouchMode="true"><requestFocus /></LinearLayout><com.widget.CustomInputBoxandroid: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><includestyle="@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" /><RelativeLayoutandroid:id="@+id/account_sex_layout"android:layout_width="match_parent"android:layout_height="50dp"android:paddingLeft="12dp"android:paddingRight="20dp" ><TextViewstyle="@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="性别" /><TextViewandroid: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="保密" /></RelativeLayout><includestyle="@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" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="40dp"android:orientation="vertical"android:paddingRight="0dp"android:paddingBottom="20dp" ><TextViewstyle="@style/setting_item_text_style"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="12dp"android:text="个性签名" /><includestyle="@style/setting_dividing_line_style"android:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginLeft="12dp"android:layout_marginRight="0dp"android:layout_marginTop="10dp"layout="@layout/widget_horizontal_dividing_line" /><EditTextandroid:id="@+id/account_description"style="@style/account_item_text_style"android:hint="用一段话介绍自己…"android:gravity="top"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:layout_marginTop="10dp"android:paddingBottom="10dp"android:background="@color/transparent"android:ellipsize="end"android:maxLines="3"android:minLines="3" /></LinearLayout></LinearLayout></ScrollView></FrameLayout>上面是我的layout,就一个比较简单的用户编辑,头部保存的textview设置了android:focusable="true" android:focusableInTouchMode="true"这两个属性,导致当按edittext获取焦点弹出软键盘后,编辑按保存的时候没有调用这个textview的setonclicklistener里面的onclick方法。

一直想不明白为什么,然后看到“当EditText的clearfocus清除焦点的时候,框架更倾向于把焦点给试图层上的第一个能够获取焦点的view”这句话,其实是那些能够获取焦点的view。

夫妇一条心,泥土变黄金。

Android中涉及到的焦点问题,focusable,clickable,enabled

相关文章:

你感兴趣的文章:

标签云: