Android how to focus ActionBar searchView

后端 未结 9 1269
野性不改
野性不改 2020-12-23 09:19

Hello i need to put some text inside the searchView and focus/expand the searchView widget.

this is what i tried but it doesn\'t work

@Override
publi         


        
9条回答
  •  旧巷少年郎
    2020-12-23 09:55

    Here is another solution.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        MenuItem menu_search = menu.findItem(R.id.menu_search);
    
    
        menu_search.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                return true;  // Return true to collapse action view
            }
    
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                //get focus
                item.getActionView().requestFocus();
                //get input method
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                return true;  // Return true to expand action view
            }
        });
        return true;
    }
    

提交回复
热议问题