C虾仔笔记 - Dialog对话框

■Dialog对话框
概述:对话框控件实现对话交流功能,提示与操作意图相关的(重要)信息,增加用户体验。

应用:

当Dialog中含有EditText时,为了增强用户体验性,在显示Dialog的同时自动弹出键盘:

方法一:

不要使用AlertDialog,而是使用Dialog即可。

如果一定要使用AlertDialog,可用如下第二种方法。

方法二:
1)、 自定义对话框,在其中增加如下方法:
public void showKeyboard() {  
   if(et!=null){
       //设置可获得焦点  
       et.setFocusable(true);  
       et.setFocusableInTouchMode(true);  
       //请求获得焦点  
       et.requestFocus();  
       //调用系统输入法  
       InputMethodManager inputManager = (InputMethodManager) et
          .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
       inputManager.showSoftInput(editText, 0);  
   } 

其中et为对话框中的输入框。
2)、执行dlg.show()后,可能对话框的界面还没加载完成,输入框可能为空,要延迟显示:
dlg.show();  
Timer timer = new Timer();  
TimerTask task = new TimerTask() {
    @Override  
    public void run() {  
if (dlg.isShowing()) {

    dlg.showKeyboard();

timer.cancel();  

  }else{
    timer.schedule(task, 100);
}
    }  
};
timer.schedule(task, 200);  

你可能感兴趣的:(Android,-,Dialog)