AlertDialog

1.AlertDialog点击时不自动消失

//在setPositiveButton和setNegativeButton根据自己的逻辑处理,大概代码如下
if(validate){//验证通过自动消失
    setDialogDismissable(dialog, true);
}else{//验证没通过,不能消失
    setDialogDismissable(dialog, false)
}

void setDialogDismissable(DialogInterface dialog, boolean dismissable){
        try {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true); 
            field.set(dialog, dismissable);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

 

2.AlertDialog显示(EditText)时,自动弹出软键盘代码如下

final EditText textEdit = new EditText(this);

// Builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//TODO:builder.setTitle("");

// Dialog
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(textEdit, InputMethodManager.SHOW_IMPLICIT);
    }
});

dialog.show();

 注意:上面代码是dialog.show(), 不是builder.show();

posted @ 2014-05-29 15:48  BaronZ  阅读(261)  评论(0编辑  收藏  举报