简体   繁体   中英

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

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.

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 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. I only run this code when the os version is Gingerbread or earlier. Otherwise, I use a 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:

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):

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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