如何从 SearchView 中移除焦点?
How to remove focus from SearchView?
我想在 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" />
在你的 onResume 方法中使用这行代码
if (searchView != null) {
searchView.setQuery("", false);
searchView.clearFocus();
searchView.onActionViewCollapsed();
}
最简单(我认为,唯一 100% 有效)的解决方案是创建一个隐形的 LinearLayout
。此 LinearLayout
将从 EditText
.
中获取焦点
将这段代码写在onResume()
searchView.setText("");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I want to remove focus and text from SearchView
in onResume()
要实现此目的,您可以使用 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();
}
在 activity 中转到父布局,在 fragment
中也在 framelayout
中做同样的事情。只需添加此属性:
android:focusableInTouchMode="true"
我刚刚请求关注其他一些视图组,它从搜索视图中删除了焦点。就像在存在搜索视图的父视图组 eg(Linearlayout, RelativeLayout) 中一样,添加
focusable = false; focusableInTouchMode=true
到父视图组。
您可以在 onResume() 函数中手动隐藏键盘:
InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
View view = context.getCurrentFocus();
if (view == null) {
view = new View(context);
}
if (view != null) {
IBinder iBinder = view.getWindowToken();
imm.hideSoftInputFromWindow(iBinder, 0);
}
对于那些希望在 Activity
启动时摆脱焦点的人 AND SearchView
在 Menu
,我做了这三件事,它可能只需要几个:
1.- 创建 SearchView
的父 class。我们称它为 RafaSearchView
:
class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: SearchView(context, attrs, defStyleAttr) {
init {
setIconifiedByDefault(false)
}
}
2.- 在 Toolbar
所在的位置获取 xml,然后设置 touchable="false"
和 focusableInTouchMode="true"
:
<androidx.appcompat.widget.Toolbar
android:id="@+id/searchToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
android:focusable="false"
android:focusableInTouchMode="true"
android:theme="@style/ToolBarStyle"
app:popupTheme="@style/AppTheme.PopupOverlay" />
3.- 在 onCreateOptionsMenu(Menu)
上,当我声明我的 RafaSearchView
时,调用:
searchView?.isIconified = false
searchView?.clearFocus()
searchView.clearFocus();
onResume()
帮助了我的案子
在您的 SearchView 小部件中,只需添加
android:focusable="false"
完整的小部件代码
<android.support.v7.widget.SearchView
android:id="@+id/your_search_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconifiedByDefault="false"
app:queryHint="Search something..."
android:focusable="false" />
这将 100% 从 SearchView 中移除焦点。
override fun onResume() {
if (searchView != null) {
searchView.clearFocus();
}
super.onResume()
}
我想在 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" />
在你的 onResume 方法中使用这行代码
if (searchView != null) {
searchView.setQuery("", false);
searchView.clearFocus();
searchView.onActionViewCollapsed();
}
最简单(我认为,唯一 100% 有效)的解决方案是创建一个隐形的 LinearLayout
。此 LinearLayout
将从 EditText
.
将这段代码写在onResume()
searchView.setText("");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
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();
}
在 activity 中转到父布局,在 fragment
中也在 framelayout
中做同样的事情。只需添加此属性:
android:focusableInTouchMode="true"
我刚刚请求关注其他一些视图组,它从搜索视图中删除了焦点。就像在存在搜索视图的父视图组 eg(Linearlayout, RelativeLayout) 中一样,添加
focusable = false; focusableInTouchMode=true
到父视图组。
您可以在 onResume() 函数中手动隐藏键盘:
InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
View view = context.getCurrentFocus();
if (view == null) {
view = new View(context);
}
if (view != null) {
IBinder iBinder = view.getWindowToken();
imm.hideSoftInputFromWindow(iBinder, 0);
}
对于那些希望在 Activity
启动时摆脱焦点的人 AND SearchView
在 Menu
,我做了这三件事,它可能只需要几个:
1.- 创建 SearchView
的父 class。我们称它为 RafaSearchView
:
class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: SearchView(context, attrs, defStyleAttr) {
init {
setIconifiedByDefault(false)
}
}
2.- 在 Toolbar
所在的位置获取 xml,然后设置 touchable="false"
和 focusableInTouchMode="true"
:
<androidx.appcompat.widget.Toolbar
android:id="@+id/searchToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
android:focusable="false"
android:focusableInTouchMode="true"
android:theme="@style/ToolBarStyle"
app:popupTheme="@style/AppTheme.PopupOverlay" />
3.- 在 onCreateOptionsMenu(Menu)
上,当我声明我的 RafaSearchView
时,调用:
searchView?.isIconified = false
searchView?.clearFocus()
searchView.clearFocus();
onResume()
帮助了我的案子
在您的 SearchView 小部件中,只需添加
android:focusable="false"
完整的小部件代码
<android.support.v7.widget.SearchView
android:id="@+id/your_search_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconifiedByDefault="false"
app:queryHint="Search something..."
android:focusable="false" />
这将 100% 从 SearchView 中移除焦点。
override fun onResume() {
if (searchView != null) {
searchView.clearFocus();
}
super.onResume()
}