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中增加如下方法:
  1. public void showKeyboard() {  
  2.         if(editText!=null){  
  3.             //设置可获得焦点  
  4.             editText.setFocusable(true);  
  5.             editText.setFocusableInTouchMode(true);  
  6.             //请求获得焦点  
  7.             editText.requestFocus();  
  8.             //调用系统输入法  
  9.             InputMethodManager inputManager = (InputMethodManager) editText  
  10.                     .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
  11.             inputManager.showSoftInput(editText, 0);  
  12.         }  
  13.     }  
editText为自定义dialog中的输入框的view
在dialog.show()后,
  1. dialog.show();  
  2. Timer timer = new Timer();  
  3. timer.schedule(new TimerTask() {  
  4.   
  5.     @Override  
  6.     public void run() {  
  7.         dialog.showKeyboard();  
  8.     }  }, 300);  

网上查这样也可以,未测试

  1. dialog.setOnShowListener(newOnShowListener(){
  2. publicvoid onShow(DialogInterface dialog){
  3. InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  4. imm.showSoftInput(et_dialog_confirmphoneguardpswd,InputMethodManager.SHOW_IMPLICIT);
  5. }
  6. });



更多相关文章

  1. Android(安卓)ApiDemos示例解析(177):Views->Lists->10. Single
  2. Android对话框--各种对话框 界面优美
  3. android 检测外接键盘并设置输入法布局
  4. android输入法问题
  5. Android(安卓)不依赖于Activity的全局对话框Dialog
  6. Android(安卓)Studio 之 对话框开发(Dialog) ---- 两种方式来实
  7. [android常见错误]Export aborted because fatal lint errors we
  8. 详解Android中Dialog的使用
  9. Android(安卓)InputMethod 教程

随机推荐

  1. Android:WebView全面总结
  2. android 6.0的变化
  3. Kivy A to Z -- 如何从Python创建一个基
  4. [置顶] 两分钟彻底让你明白Android(安卓)
  5. App 调试的几个命令实践
  6. Android浪潮
  7. Android使用Fragment实现底部导航栏
  8. [原创] 在Android系统使用socket在framew
  9. Android中ListView结合CheckBox判断选中
  10. Android(安卓)java层音频相关的分析与理