简体   繁体   English

Android搜索对话框软键盘保持打开状态的时间过长

[英]Android Search Dialog soft keyboard stays open for too long

I'm trying to use Android's built-in Search Dialog , and it's working fine except when I try to use it with a ProgressDialog . 我正在尝试使用Android的内置“ 搜索对话框” ,除非我尝试将其与ProgressDialog一起使用,否则它的运行情况都很好。 The basic chain of events it his: 它的基本事件链是:

  1. User press the search button, enters a query and submits it. 用户按下搜索按钮,输入查询并提交。
  2. The SearchManager calls the search Activity's onCreate method. SearchManager调用搜索活动的onCreate方法。
  3. In the onCreate method, I call an AsyncTask that runs a query and shows a ProgressDialog in the onPreExecute method and hides it in the onPostExecute method. 在onCreate方法中,我调用一个AsyncTask来运行查询,并在onPreExecute方法中显示一个ProgressDialog并将其隐藏在onPostExecute方法中。

This all happens fine except as soon as I submit the query the screen looks like this: 一切正常,除非我提交查询后,屏幕如下所示:

在此处输入图片说明

... which is pretty ugly. ...这很丑。 How can I prevent this from happening? 我怎样才能防止这种情况发生?

You can find the source code for the project at Google Code. 您可以在Google Code中找到该项目的源代码。

I had a similar issue with hiding the keyboard after performing or canceling a search. 在执行或取消搜索后隐藏键盘也遇到类似的问题。 The problem is that you cannot get a reference to the SearchManager's Search Dialog, or the edit text (so your usual hideSoftInputFromWindow method will not work.) If you run the following code as part of your set-up, it should take care of your issue: 问题是您无法获得对SearchManager的“搜索对话框”的引用,也无法获得编辑文本的引用(因此,通常的hideSoftInputFromWindow方法将不起作用。)如果在安装过程中运行以下代码,则应注意以下事项:问题:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchManager.setOnDismissListener(new OnDismissListener() {

    @Override
    public void onDismiss() {
        final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, 0);
    }
});

It is worth noting that toggling the keyboard may not be the right thing to do on all versions of Android. 值得注意的是,在所有版本的Android上切换键盘可能都不是正确的选择。 I only run this code when the os version is Gingerbread or earlier. 我仅在os版本为Gingerbread或更早版本时运行此代码。 Otherwise, I use a SearchView. 否则,我将使用SearchView。

I know it's late but it might be still useful. 我知道已经晚了,但可能仍然有用。 If you use SearchDialog which you can't access its SearchManager (eg Android 2) you can dismiss the soft keyboard with the following: 如果您使用无法访问其SearchManager(例如Android 2)的SearchDialog,则可以使用以下方法关闭软键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);

or if you can access the edit text (eg SearchView in Android 4 or any EditText): 或是否可以访问编辑文本(例如,Android 4中的SearchView或任何EditText):

imm.hideSoftInputFromWindow(YOUR_EDIT_TEXT.getWindowToken(), 0);

I came across the same problem and "solved" it by delaying the progress dialog display using a Handler 我遇到了相同的问题,并通过使用Handler延迟了进度对话框的显示来“解决”了它

in your activity, create handler that simply show a previously created progress dialog: 在您的活动中,创建仅显示先前创建的进度对话框的处理程序:

private final Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        if (null != mProgress) {
            mProgress.show();
        }
    }
};

then use the following method to show or hide it : 然后使用以下方法显示或隐藏它:

private void showWaitPopup(final boolean doShow) {
    if (doShow) {
        if (null == mProgress) {
            mProgress = new ProgressDialog(this);
            mProgress.setMessage(getString(R.string.searching));
            mProgress.setCancelable(false);

            // launch timer here
            mHandler.sendEmptyMessageDelayed(0, 100);
        }
    } else {
        if (null != mProgress) {
            mProgress.dismiss();
            mProgress = null;
        }
    }
}

have you tried below code might help 您是否尝试过以下代码可能会有所帮助

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

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

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