本文翻译自:Android: Force EditText to remove focus? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
I would like to be able to remove the focus from the EditText. 我希望能够从EditText中删除焦点。 For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. 例如,如果键盘出现,并且用户使用后退按钮隐藏它,我希望焦点和光标消失。 How can it be done? 怎么做到呢?
#1楼
参考:https://stackoom.com/question/LDUE/Android-强制EditText删除焦点-重复
#2楼
check your xml file
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" >
**<requestFocus />**
</EditText>
//Remove **<requestFocus />** from xml
#3楼
Add LinearLayout
before EditText
in your XML. 在XML中的EditText
之前添加LinearLayout
。
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
Or you can do this same thing by adding these lines to view before your 'EditText'. 或者你可以通过在“EditText”之前添加这些行来查看相同的内容。
<Button
android:id="@+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="@color/blue"
android:textSize="13sp"
android:textStyle="bold" />
#4楼
You can also include android:windowSoftInputMode="stateAlwaysHidden"
in your manifest action section. 您还可以在清单操作部分中包含android:windowSoftInputMode="stateAlwaysHidden"
。
This is equivalent to: 这相当于:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
#5楼
Remove focus but remain focusable: 删除焦点但仍保持专注:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText will lose focus, but can gain it again on a new touch event. EditText将失去焦点,但可以在新的触摸事件中再次获得它。
#6楼
You can avoid any focus on your elements by setting the attribute android:descendantFocusability of the parent element. 通过设置父元素的属性android:descendantFocusability ,可以避免对元素的任何关注。
Here is an example: 这是一个例子:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
Here, the attribute android:descendantFocusability set to "blocksDescendants" is blocking the focus on the child elements. 这里,设置为“blocksDescendants”的属性android:descendantFocusability阻止了对子元素的关注。