您的位置:首页 > 移动开发 > Android开发

Android弹出式对话框AlertDialog中的EditText自动打开软键盘

2016-09-19 17:43 489 查看
Activity中需要启动一个AlertDialog,这个对话框使用的是自定义布局,在这个对话框里有个EditText,可能是自定义布局的问题,导致对话框弹出时不能自动打开软键盘并定位焦点到文本框里。
dialog.show();
dialog.setContentView(windowLayout);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
show之后设置红色代码AlertDialog里的Edittext可获得焦点在dialog 里findViewById,拿到Edittext,requestFocus也不好使,因为dialog还没有完全展示到屏幕上,类似于Activity还没有执行OnResume。解决方法:延迟一会儿调出输入法可以在自定义的dialog中增加如下方法:public void showKeyboard() {          if(editText!=null){              //设置可获得焦点              editText.setFocusable(true);              editText.setFocusableInTouchMode(true);              //请求获得焦点              editText.requestFocus();              //调用系统输入法              InputMethodManager inputManager = (InputMethodManager) editText                      .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);              inputManager.showSoftInput(editText, 0);          }      }  editText为自定义dialog中的输入框的view在dialogbc3f.show()后,dialog.show();  Timer timer = new Timer();  timer.schedule(new TimerTask() {        @Override      public void run() {          dialog.showKeyboard();      }  }, 300); 
网上查这样也可以,未测试
dialog.setOnShowListener(newOnShowListener(){publicvoid onShow(DialogInterfacedialog){InputMethodManager imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(et_dialog_confirmphoneguardpswd,InputMethodManager.SHOW_IMPLICIT);}});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐