5

I have a project that targets api 11 and above, so no actionbarsherlock.

I'm starting an activity with a SearchView. I want to focus the SearchView immediately, and have the soft keyboard up (if there's no hardware keyboard). I've tried:

mSearchView.requestFocus();
mSearchView.requestFocusFromTouch();

which does in fact focus the searchview, but it doesn't show the keyboard. I've read through other posts saying it's worked. Other posts try to force the keyboard up using:

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

which will show the keyboard, but then it behaves strangely after I submit a search query - it hides itself, then pops up again, then hides itself finally.

Anyone have a correct way of getting this to work?

Thanks

-------- Edit ----------------

Here's how I'm setting it up:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    ...     

    MenuItem searchItem = menu.findItem(R.id.action_search); 
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setIconifiedByDefault(false);

    if (!mDidThisOnce) {
        mSearchView.requestFocusFromTouch();
        InputMethodManager imm = ...;
        imm.toggleSoftInput(...);
        mDidThisOnce = true;
    }
}

------ Last Edit -----------

So this works, but I needed to keep a flag to make sure I didn't run the imm code more than once, otherwise it keeps popping the keyboard up after you submit a search.

4

1 回答 1

1

我有同样的问题。

我以前用过searchView.clearFocus();,这对我有用。

于 2014-02-19T14:27:55.110 回答