开发者

Android Search Dialog soft keyboard stays open for too long

开发者 https://www.devze.com 2023-02-13 03:29 出处:网络
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开发者_Python百科:

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开发者_Python百科:

  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:

Android Search Dialog soft keyboard stays open for too long

... which is pretty ugly. How can I prevent this from happening?

You can find the source code for the project at Google Code.


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


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 (e.g. 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 (e.g. SearchView in Android 4 or any EditText):

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


have you tried below code might help

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

精彩评论

暂无评论...
验证码 换一张
取 消