Default Focus and Keyboard to EditText in Android AlertDialog

前端 未结 5 1847
醉酒成梦
醉酒成梦 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:57

    use a custom view if you need it more often

    public class RequestFocusEditText extends AppCompatEditText {
        private RequestFocusEditText self;
    
        public RequestFocusEditText(final Context context, AttributeSet attrs) {
            super(context, attrs);
            this.self = this;
    
            setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    post(new Runnable() {
                        @Override
                        public void run() {
                        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        });
        requestFocus();
        }
    }
    

提交回复
热议问题