我想从 onResume()
中的 SearchView
中移除焦点和文本。
我试过 searchView.clearFocus()
但它不起作用。
这是我的 xml 代码:
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toolbar"
android:layout_alignTop="@+id/toolbar"
android:layout_centerVertical="true"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:iconifiedByDefault="false"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
最佳答案
I want to remove focus and text from
SearchView
inonResume()
要实现这一点,您可以使用 android:focusableInTouchMode
属性将您的根布局设置为可聚焦,并在 onResume()
中请求将焦点放在该 View
上.
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"/>
</LinearLayout>
然后在你的Activity
中:
private View rootView;
private SearchView searchView;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
rootView = findViewById(R.id.root_layout);
searchView = (SearchView) findViewById(R.id.search_view);
}
@Override
protected void onResume() {
super.onResume();
searchView.setQuery("", false);
rootView.requestFocus();
}
关于android - 如何从 SearchView 中移除焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411341/