Showing the soft keyboard for SearchView on Action

2019-03-25 09:35发布

We've got a SearchView on the ActionBar which is set to be non-iconified. As we don't have any content in the view until the user's entered something to search for, it would make sense to give the SearchView initial focus, and make sure the soft keyboard is showing ready for the user to enter text — otherwise they'll always have to first tap in the SearchView.

I can give the SearchView focus by just calling

searchView.requestFocus();

but I can't get the soft keyboard to appear. In another one of our Fragments I have an EditText which we want to be focused I can get the soft keyboard to appear there by calling

InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

but this just doesn't work on the SearchView. It must surely be possible to get this to work.

7条回答
时光不老,我们不散
2楼-- · 2019-03-25 10:07

If you wanna show the soft keyboard and focus on the input box, you can try

final MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.expandActionView();//expand show soft keyboard

 <item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_actionbar_search"
    liven:showAsAction="collapseActionView|always" //always
    liven:actionViewClass="android.support.v7.widget.SearchView" />enter code here
查看更多
登录 后发表回答