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

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

I have a layout which contains some views like this:






&         


        
13条回答
  •  执笔经年
    2020-11-28 02:32

    I couldn't get any of these answers to work on their own. The solution for me was to combine them:

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    editText.requestFocus();
    imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    

    I'm not sure why that was required for me -- according to the docs it seems that either method should have worked on their own.

提交回复
热议问题