1. 程式人生 > >(實用)dialog中點選按鈕禁止其消失

(實用)dialog中點選按鈕禁止其消失

new AlertDialog.Builder(this).setTitle("請輸入您的工種").setView(et_text).setPositiveButton("新增", new DialogInterface.OnClickListener() {
    @Override
public void onClick(DialogInterface dialog, int which) {
        //dialogedittext中的內容儲存起來
if(!TextUtils.isEmpty(et_text.getText())){
            newAddTag 
= et_text.getText().toString(); dialog.dismiss(); }else{ Toast.makeText(UserGongZhongActivity.this,"沒有輸入內容",Toast.LENGTH_SHORT).show(); //不讓dialog顯示 try { Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); //mShowing設定為false
,欺騙系統說 對話方塊已經關閉了; field.set(dialog,false); dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } } } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show();
具體原理,請看   http://blog.csdn.net/jdsjlzx/article/details/25288749 大神寫的文章
//每個按鈕都要設定,否則點選任何按鈕不消失