Android:强制EditText删除焦点? [重复]

当用户使用后退按钮隐藏键盘时,如何使EditText失去焦点并隐藏光标?在XML布局中添加 或通过编程方式处理,以防止EditText在Activity启动时自动获取焦点。设置父元素的android:descendantFocusability属性为"blocksDescendants",可以阻止子元素获取焦点。
摘要由CSDN通过智能技术生成

本文翻译自: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阻止了对子元素的关注。

You can find more info here . 你可以在这里找到更多信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值