Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从SearchView中删除焦点?_Android_Focus_Searchview - Fatal编程技术网

Android 如何从SearchView中删除焦点?

Android 如何从SearchView中删除焦点?,android,focus,searchview,Android,Focus,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/tool

我想从
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
将从
EditText

中获取焦点,并将此代码写入
onResume()

我想从
onResume()中的
SearchView
中删除焦点和文本

要实现这一点,您可以使用
android:focusableInTouchMode
属性设置根布局的焦点,并在
onResume()中请求对该
视图的焦点

例如:

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

在活动中,转到父布局,在
片段中,也在
框架布局中执行相同的操作。只需添加此属性:

android:focusableInTouchMode="true"

我刚刚向其他视图组请求了焦点,它从searchview中删除了焦点。与存在searchview的父视图组例如(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);
         }

对于那些希望在
活动启动且
菜单中有
搜索视图
时摆脱焦点的人,我做了以下三件事,可能只需要几个:

1.-创建
SearchView
的父类。我们称之为搜索视图

class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : SearchView(context, attrs, defStyleAttr) {

    init {
        setIconifiedByDefault(false)
    }
}
2.-在
工具栏的位置获取xml,并设置
touchable=“false”
focusableInTouchMode=“true”


只需
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" />


它怎么不起作用?导致某种错误还是什么都没发生?请更具描述性…显示您的代码好友:)@Vucko实际上它没有显示任何错误,光标也没有删除。它正在禁用我不想要的focusListner,我只想在点击它时返回。当碎片再次加载时,它应该被移除。你们的问题和你们现在解释的不一样。(我想在onResume()中从SearchView中删除焦点和文本。我尝试了SearchView.clearFocus();但它不起作用。)如果在onResume中从SearchView中删除焦点(或转移),我们将看到一些手机的键盘闪烁了一秒钟。在onPause中清除焦点使其看起来更平滑。如果您有适配器,则需要重复检查两次。否则,您的应用程序可能会崩溃。嗯,这似乎可以正常工作。让我想知道为什么所有其他答案都更复杂。对我来说,这应该是公认的答案,对于焦点部分。摘自“可聚焦”文档:控制视图是否可以聚焦。默认情况下,这是“自动”,它允许框架确定用户是否可以将焦点移动到视图。通过将此属性设置为true,可以使视图获得焦点。通过将其设置为“false”,视图将不会聚焦。此值不影响直接调用{@link android.view.view#requestFocus}的行为,无论此视图如何,它都会请求焦点。它只影响焦点导航尝试移动焦点的位置。
    <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" />
    searchView?.isIconified = false
    searchView?.clearFocus()
  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" />