How can I focus on a collapsible action view EditText item in the action bar (when it is expanded) and force the soft keyboard to open?

跟風遠走 提交于 2019-11-30 08:12:31

Maybe the requestFocus never occurred in the code in the question because the searchText was not expanded yet? I don't know, anyway... this worked for me:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    searchText.post(new Runnable() {
        @Override
        public void run() {
            searchText.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
        }
    });
    return true; // Return true to expand action view
}
Tadas Valaitis

Works for me too!

For Collapsing ActionView and hiding Keyboard I use:

Set Listener to capture search button (or any other) press on soft keyboard, then use collapseActionView():

searchText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            searchMenuItem.collapseActionView();
            return true;
        }
        return false;
    }
});

Then write keyboard hiding code in onMenuItemActionCollapse() method.

public boolean onMenuItemActionCollapse(MenuItem item) {
    // Do something when collapsed
    searchText.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
        }
    });
    return true; // Return true to collapse action view
}

For an easier implementation of a collapsible search item in your ActionBar, you may simply use SearchView which is also available in ActionBarSherlock (com.actionbarsherlock.widget.SearchView).

It comes with many handy methods and handles showing/hiding the software keyboard on expanding/collapsing the view automatically. Plus, it is easier to define what to do on submitting the search. Just take a look at all the listeners you can set on that SearchView.

Here's an example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    SearchView searchView = new SearchView(this);

    searchView.setQueryHint(getString(R.string.search_hint));
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // what to do on submit, e.g. start an Activity and pass the query param
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

    });

    menu.add(getString(R.string.search_title))
        .setIcon(R.drawable.ic_action_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    // ...place to add other menu items

    return super.onCreateOptionsMenu(menu);
}

Another good reason: I had some layout issues on older devices when using a custom EditText as my search view (like you did in your question). The default SearchView solved all these issues and saved me many lines of code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!