简体   繁体   English

如何检测何时关闭了SearchView的虚拟键盘

[英]How to detect when virtual keyboar of a SearchView is dismissed

I wanna call mSearchView.clearFocus() when virtual keyboard is dismissed, how to do that? 我想在虚拟键盘关闭时调用mSearchView.clearFocus(),该怎么做?

My problem is once the SearchView gets focused, it keeps focused, so if I dismissed the virtual keyboard using back button, and I opened an AlertDialog - for example - the virtual keyboard pops up again once I close the AlertDialog as the search view still has the focus, as if it regains focus. 我的问题是SearchView变得聚焦后,它会保持聚焦,因此,如果我使用“后退”按钮解雇了虚拟键盘,并且打开了AlertDialog-例如,当我关闭AlertDialog时,由于搜索视图仍然存在,虚拟键盘再次弹出。焦点,好像它重新获得了焦点。

for the SearchView I used: 对于我使用的SearchView:

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

for the activity holds the SearchView I use: 该活动包含我使用的SearchView:

android:windowSoftInputMode="stateUnspecified|adjustPan"

even if I changed it to the following I get the same problem 即使我将其更改为以下内容,我也会遇到相同的问题

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

Eidt 1: 开斋节1:

changing 改变中

    android:iconifiedByDefault="false"

to be 成为

    android:iconifiedByDefault="true"

doesn't solve the problem, I get the same result. 无法解决问题,我得到相同的结果。

Edit 2: 编辑2:

I tried the approach of creating a custom SearchView and to override onKeyPreIme and call clearFocus(), but onKeyPreIme doesn't get called. 我尝试了创建自定义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);

    }
}

To hide keyboard use this 要隐藏键盘,请使用此

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

and then in you onBackPressed() 然后在你onBackPressed()

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

} }

while rootView is rootView

rootView = findViewById(R.id.root_layout);

I have tried adding a searchview to a linerlayout and i do not have the same problem like you. 我试图将searchview添加到衬里布局,但我没有像您一样的问题。 But if you want to track virtual keyboard hide event use the following code in onCreate() 但是,如果您想跟踪虚拟键盘的隐藏事件,请在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 is root LinearLayout view of activity mLLWrapper是活动的根LinearLayout视图

Once the keyboard is dismissed call clear focus. 关闭键盘后,请呼叫清除焦点。 That might help. 这可能会有所帮助。 If not update your question with more code which will be easy for us to help you. 如果没有,请使用更多代码更新您的问题,这将很容易为我们提供帮助。

try this way 尝试这种方式

internal class ProductSearchView(context: Context, attrs: AttributeSet) : SearchView(context, attrs) {

    override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
       return false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM