Default Focus and Keyboard to EditText in Android AlertDialog

前端 未结 5 1851
醉酒成梦
醉酒成梦 2021-02-12 09:24

I am using the AlertDialog.Builder in Android to quickly prompt the user for text. The dialog shows up and works great, but the user must click on the EditText field to load th

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-12 09:48

    Use the following code. It worked for me.

        editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                editText.post(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        });
        editText.requestFocus();
    

提交回复
热议问题