質問

Even if my SearchView is not in focus (ie. the user has already pressed the "Search" button earlier to submit their query), when I press the cancel (the X) button on my Android SearchView, the soft keyboard comes back up into view.

My thinking is that if the user doesn't already have the keyboard on the screen, then they just want to clear the filter/search box. If they want to clear the filter and type something different they can tap it again.

However, if they are typing into the box and make a mistake I would expect the keyboard to remain in view (because the search view already has focus).

So in a nutshell I want:

  1. If the user is typing in the search view and taps cancel/clear, then the keyboard stays in focus.
  2. If the user is not currently typing in the search view (ie. the keyboard has disappeared from view), then tapping the clear button should just clear the query and NOT bring the keyboard back into view.

I know I can use the setOnCloseListener() event to hook into when the clear/close button is pressed, but I don't know how to stop it from showing the soft keyboard as mentioned in point #2.

EDIT:

Maybe there is a way I can have the search view "lose focus"? How do I achieve this result? Thanks.

役に立ちましたか?

解決

You can lose focus by doing the following:

searchView.clearFocus();

You can also force hiding the keyboard on any event you want with the inputManager.

For example:

InputMethodManager inputManager = (InputMethodManager) this
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    //check if no view has focus:
    View v=this.getCurrentFocus();
    if(v==null)
        return;

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top