【问题标题】:How to detect when virtual keyboar of a SearchView is dismissed如何检测 SearchView 的虚拟键盘何时被关闭
【发布时间】:2018-02-13 13:46:50
【问题描述】:

我想在虚拟键盘关闭时调用 mSearchView.clearFocus(),怎么做?

我的问题是一旦 SearchView 获得焦点,它就会保持焦点,所以如果我使用后退按钮关闭虚拟键盘,并打开一个 AlertDialog - 例如 - 一旦我关闭 AlertDialog 作为搜索,虚拟键盘就会再次弹出视图仍然具有焦点,就好像它重新获得焦点一样。

对于我使用的 SearchView:

    android:iconifiedByDefault="false"
    android:focusable="false"

对于活动持有我使用的 SearchView:

android:windowSoftInputMode="stateUnspecified|adjustPan"

即使我将其更改为以下内容,我也会遇到同样的问题

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

开斋节 1:

改变

    android:iconifiedByDefault="false"

成为

    android:iconifiedByDefault="true"

没有解决问题,我得到了同样的结果。

编辑 2:

我尝试了创建自定义 SearchView 并覆盖 onKeyPreIme 并调用 clearFocus() 的方法,但没有调用 onKeyPreIme。

public class ModifiedSearchView extends SearchView {
    public ModifiedSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onKeyPreIme (int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            clearFocus();
            return false;
        }

        return super.dispatchKeyEvent(event);

    }
}

【问题讨论】:

  • 你在用menu.xml
  • 不,我在包含其他视图的 LinearLayout 中有 SearchView
  • 我的活动来源于AppCompatActivity
  • 这个插孔有什么更新吗?

标签: android android-virtual-keyboard


【解决方案1】:

要隐藏键盘使用这个

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

然后在你onBackPressed()

if (searchView != null) {
searchView.setQuery("", false);
searchView.clearFocus();
rootView.requestFocus();

}

rootView

rootView = findViewById(R.id.root_layout);

【讨论】:

  • 谢谢,它提供了相同的结果,最糟糕的是,我希望 SearchView 在 Activity 开始时聚焦(但不显示键盘),这是 android:iconifiedByDefault="false" 实现的
  • 我们通常将searchView设置为工具栏作为 menue_options ,并且可以更好地控制它的显示和隐藏。
  • 谢谢,但我不想手动隐藏键盘,我只想强制它失去焦点——以避免重新打开键盘——当用户使用返回键关闭它时
  • 我建议您将其用作工具栏中的选项菜单,您将获得准确的输出,我正在以相同的方式使用它。
  • 谢谢,但根据设计,它不可能出现在工具栏中,因为没有工具栏,它是一个全屏活动。
【解决方案2】:

我尝试将搜索视图添加到 linerlayout,但我没有像您一样的问题。但是如果你想跟踪虚拟键盘隐藏事件,请在 onCreate() 中使用以下代码

mLLWrapper.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mLLWrapper.getWindowVisibleDisplayFrame(r);

                int heightDiff = mLLWrapper.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 300) { // if more than 100 pixels, its probably
                    // keyboard visible
                } else {
                    // keyboard in not visible
                }
            }
        });

mLLWrapper 是活动的根 LinearLayout 视图

一旦键盘被关闭,调用清除焦点。这可能会有所帮助。如果不使用更多代码更新您的问题,我们将很容易为您提供帮助。

【讨论】:

    【解决方案3】:

    试试这个方法

    internal class ProductSearchView(context: Context, attrs: AttributeSet) : SearchView(context, attrs) {
    
        override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
           return false
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 2011-11-16
      • 2020-03-26
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多