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?

后端 未结 3 836
旧巷少年郎
旧巷少年郎 2020-12-31 01:50

I am using Jake Wharton\'s excellent ActionBarSherlock library and have a collapsible search action view. I want to popup the soft keyboard when the search action view is ex

3条回答
  •  囚心锁ツ
    2020-12-31 02:36

    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
    }
    

提交回复
热议问题