简体   繁体   中英

Show soft keyboard on search actionbar activation with SearchView

I see this question sets focus on the SearchView EditText when I activate a search from the ActionBar. However the keyboard does not come up when it gains focus. Shouldn't it, as it is just a normal EditText? (Is it a normal EditText?) This behaviour is seen on Android SDK level 11. (Samsung Galax Tab 7.7 with stock Android.)

I have a workaround at the moment that hooks in on the onOptionsItemSelected(MenuItem item) method of my Activity, showing the keyboard.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean menuSelectionHandeled = super.onOptionsItemSelected(item);
        // menu_search is the id of the menu item in the ActionBar
        if (item.getItemId() == R.id.menu_search) {
               mInputManager.showSoftInput(null, InputMethodManager.SHOW_IMPLICIT);
        }
        return menuSelectionHandeled;
    }

Where mInputManager is an instance of InputMethodManager .

The ActionBar is built with ActionBarSherlock, and since the target device is Android 3.x could this be the cause of the symptoms? As per ActionBarSherlock's FAQ :

The action bar on Android 3.x (also known as Honeycomb) does not implement all of the features of the one in Android 4.x (Ice Cream Sandwich). In order to provide a full action bar API on all platforms as well as unify the styling across all versions of Android the custom implementation is used.

This should work :

SearchView searchView = new SearchView(getContext());
searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.setBackgroundColor(Color.WHITE);

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