How can I set the focus (and display the keyboard) on my EditText programmatically

后端 未结 13 812
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:22

I have a layout which contains some views like this:






&         


        
13条回答
  •  余生分开走
    2020-11-28 02:29

    This worked for me, Thanks to ungalcrys

    Show keyboard:

    editText = (EditText)findViewById(R.id.myTextViewId);
    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
    

    Hide keyboard:

    InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    

提交回复
热议问题