我希望能够从EditText删除焦点。例如,如果键盘出现,用户用后退按钮隐藏它,我希望焦点和光标消失。怎样才能做到呢?


当前回答

你可以通过设置父元素的属性android:descendantFocusability来避免对元素的任何关注。

这里有一个例子:

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

在这里,属性android:descendantFocusability设置为“blocksDescendants”是阻止对子元素的关注。

你可以在这里找到更多信息。

其他回答

你可以把这个添加到onCreate,它将隐藏键盘每次活动启动。

您还可以通过编程方式将焦点更改为另一个项目。

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

您可以使光标和焦点消失

edittext.clearFocus();

但检测时隐藏键盘是一项艰苦的工作。

将这两个属性添加到你的父布局(例如:线性布局,相对布局)

android:focusable="true"
android:focusableInTouchMode="true" 

它会成功的:)

试着在你的视图上使用这个 这招对我很管用:

<View
    android:id="@+id/fucused"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"/>
editText.setFocusableInTouchMode(true)

EditText将能够在用户触摸它时获得焦点。 当主布局(活动,对话框等)变得可见时,EditText不会自动获得焦点,即使它是布局中的第一个视图。