开发者

SearchView focused when hiding the ActionBar menu

开发者 https://www.devze.com 2023-04-09 18:13 出处:网络
I have an ActionBar with a SearchView.The hidden/overflow menu items are shown on the right in a drop-down list when the menu button is selected.When the drop-down menu is hidden, the SearchView is fo

I have an ActionBar with a SearchView. The hidden/overflow menu items are shown on the right in a drop-down list when the menu button is selected. When the drop-down menu is hidden, the SearchView is focused and the keyboard shows. Is there a way to stop the keyboard from showing (except for the case where the SearchView is touched)?

Regards, Julius.

Edit added code below: Here is how I initialise it:

        SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
        ((SearchView) mSearchView).setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));

        mSearchView.setIconifiedByDefault(mIconified);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnCloseListener(this);
        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        if(null!=mQuery)
            mSearchView.setQuery(mQuery, false);

Edit 2:

Here is what I do when the user wants to start the search:

    @Override
    public boolean onQueryTextSubmit(String query) {
        // Hide keyboard
        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
                FragmentActivity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mSearchView.getWindowToken(),开发者_Go百科 0);

...

        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);

        return true;
    }


try calling mSearchView.clearFocus() on your initialization of the searchView. That worked for me when I had a similar problem.


try call setFocusable(false) on SearchView when you init it.


I instantiated a search view when creating the options menu in a fragment and none of the above methods worked for me.

However, I was able to solve the problem by holding a reference to the SearchView, NOT setting the SearchView's iconified setting in onCreateOptionsMenu(), but setting it on a post when the fragment resumed like follows:

@Override
public void onResume() {
    super.onResume();
    getView().post(new Runnable() {
        public void run() {
            if (mFilterView.isIconfiedByDefault()) {
                mFilterView.setIconifiedByDefault(false);
                mFilterView.clearFocus();
            }
        }
    });
}


Call requestFocus() for another view, ideally your results view, e.g.

_resultsFragment.getView().requestFocus();


I have just been pulling my hair out over the same issue.

I tried android:focusable="false". I tried the InputMethodManager. I tried creating a dummy view to take the focus but nothing worked.

I finally solved it with the following code inside onCreateOptionsMenu():

final ViewTreeObserver observer = mSearchView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() {
        observer.removeOnGlobalLayoutListener(this);
        mSearchView.clearFocus();
    }
});

The issue was that at every point in the Fragment's lifecycle the menu had not been created yet. The onCreateOptionsMenu() method was being called after onResume(), but only on some devices I tested on, which is really strange.

The above code makes sure that clearing the focus is done after any focus changes during creation of the page.

I hope this helps someone else, it would have saved me a lot of time to know this from the start.


When you set up your SearchView in your XML layout file (if you're using one), just use this:

android:focusable="false"

With this approach, your SearchView won't gain focus until you touch it...no matter where or if you "init" it in your code, or you hide your menu, or any other activity that might not have occurred in your debugging. This also eliminates the possible need to track where you are calling

setFocusable(false) //essentially does the same thing

in your code multiple times as the accepted answer suggests.

0

精彩评论

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

关注公众号