【问题标题】:How to remove focus from SearchView?如何从 SearchView 中移除焦点?
【发布时间】:2016-11-19 13:30:47
【问题描述】:

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

我尝试了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" />

【问题讨论】:

  • 怎么不工作了?导致某种错误或什么也没发生?请更具描述性...
  • 显示你的代码伙伴 :)
  • @Vucko 实际上它没有显示任何错误并且光标没有移除。

标签: android focus searchview


【解决方案1】:

我想从SearchView 中删除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();
}

【讨论】:

  • 如果我们在 onResume 中从 SearchView 中移除焦点(或转移),我们会在某些手机中看到键盘闪烁一秒钟。在 onPause 中清除焦点使它看起来更流畅。
  • 如果您有适配器,则需要仔细检查两次。否则,您的应用程序可能会崩溃。
【解决方案2】:

在活动中转到父布局,在fragment 中也可以在framelayout 中做同样的事情。只需添加此属性:

android:focusableInTouchMode="true"

【讨论】:

    【解决方案3】:

    在你的 onResume 方法中使用这行代码

    if (searchView != null) {
        searchView.setQuery("", false);
        searchView.clearFocus();
        searchView.onActionViewCollapsed();
    }
    

    【讨论】:

    • 它正在禁用我不想要的我的 focusListner,我只想在点击它时返回。当片段再次加载时,它应该被删除。
    • 你的问题和你现在解释的不一样。 (我想在 onResume() 中从 SearchView 中删除焦点和文本我尝试了 searchView.clearFocus(); 但它不起作用。)
    【解决方案4】:

    在您的 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" />
    

    【讨论】:

    • 嗯,这似乎工作得很好。让我想知道为什么所有其他答案都更复杂。
    • 对我来说,这应该是焦点部分的公认答案。摘自有关“可聚焦”的文档:控制视图是否可以聚焦。默认情况下,这是“自动”,它让框架确定用户是否可以将焦点移动到视图。通过将此属性设置为 true,视图可以获取焦点。通过将其设置为“false”,视图将不会获得焦点。此值不会影响直接调用 {@link android.view.View#requestFocus} 的行为,无论此视图如何,它都会始终请求焦点。它只会影响焦点导航尝试移动焦点的位置。
    【解决方案5】:

    将此代码写入onResume()

      searchView.setText("");    
      this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     
    

    【讨论】:

      【解决方案6】:

      searchView.clearFocus(); 中的 onResume()帮助了我的情况

      【讨论】:

        【解决方案7】:

        我刚刚向其他视图组请求焦点,它从搜索视图中删除了焦点。就像在存在searchview的父视图组例如(Linearlayout,RelativeLayout)中一样,添加

        focusable = false; focusableInTouchMode=true
        

        到父视图组。

        【讨论】:

          【解决方案8】:

          最简单(我认为,唯一 100% 有效)的解决方案是创建一个不可见的 LinearLayout。这个LinearLayout 将从EditText 中获取焦点。

          【讨论】:

            【解决方案9】:

            您可以在 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);
                     }
            

            【讨论】:

              【解决方案10】:

              对于那些希望在Activity 启动时摆脱焦点并且SearchView 在Menu 中的人,我做了这三件事,它可能适用只是几个:

              1.- 创建SearchView 的父类。我们就叫它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()
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2010-11-11
                • 1970-01-01
                • 2016-10-17
                相关资源
                最近更新 更多