首页 > 代码库 > Android设置AlertDialog打开后不消失

Android设置AlertDialog打开后不消失

最近项目收尾,一堆bug要改,还要对用户操作体验做一些优化,也是忙的不行。周末难得清闲,出去逛逛,看看风景,还好因为习大大要来,南京最近的天气还不错,只是苦了当地的不少农民工,无活可干,无钱可拿。想想觉得自己已经算是幸运的了,心存感激,好好工作。

原文地址请保留 http://www.cnblogs.com/rossoneri/p/4150001.html 

整理一下以前查过的东西,可惜笔记本没记多少资料博客的网址,没法贴源地址。

关于本文,是要满足一个需求:

打开一个自定义对话框之后,再点击按钮,希望弹出一个确认之类的对话框显示在前端,同时不希望原来的对话框消失。

方法就是用反射机制获取相关字段进行设置:

Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");field.setAccessible(true);field.set(dialog, false);

关于getDeclaredField()往后再研究下。

写了个demo:

 1 public class MainActivity extends ActionBarActivity { 2  3     Button btn; 4  5     @Override 6     protected void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.activity_main); 9 10         btn = (Button) findViewById(R.id.btn);11         btn.setOnClickListener(new OnClickListener() {12 13             @Override14             public void onClick(View v) {15                 // TODO Auto-generated method stub16                 AlertDialog.Builder builder = new Builder(MainActivity.this,17                         AlertDialog.THEME_HOLO_LIGHT);18                 builder.setTitle("Test");19                 builder.setMessage("Try to push the OK button and the dialog won`t dismiss do it now !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");20                 builder.setPositiveButton("OK",21                         new DialogInterface.OnClickListener() {22 23                             @Override24                             public void onClick(DialogInterface dialog,25                                     int which) {26                                 // TODO Auto-generated method stub27                                 try {28                                     Field field = dialog.getClass()29                                             .getSuperclass()30                                             .getDeclaredField("mShowing");31                                     field.setAccessible(true);32                                     field.set(dialog, false); // 此处设为true则可以关闭33                                 } catch (Exception e) {34                                     e.printStackTrace();35                                 }36 37                                 AlertDialog.Builder confirmDlg = new Builder(38                                         MainActivity.this,39                                         AlertDialog.THEME_HOLO_DARK);40                                 confirmDlg.setTitle("confirm");41                                 confirmDlg42                                         .setMessage("Do you want to do the action?");43                                 confirmDlg.create().show();44 45                             }46                         });47 48                 builder.setNegativeButton("Cancel",49                         new DialogInterface.OnClickListener() {50 51                             @Override52                             public void onClick(DialogInterface dialog,53                                     int which) {54                                 // TODO Auto-generated method stub55                                 try {56                                     Field field = dialog.getClass()57                                             .getSuperclass()58                                             .getDeclaredField("mShowing");59                                     field.setAccessible(true);60                                     field.set(dialog, true);61                                 } catch (Exception e) {62                                     e.printStackTrace();63                                 }64                             }65                         });66 67                 builder.create().show();68             }69         });70 71     }72 }

 

Android设置AlertDialog打开后不消失