android AlertDialog设置点击button不退出(转)

转载于http://blog.csdn.net/csh159/article/details/8127280

 

使用的是java里面的反射机制:

dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("输入用户名:");
dialog.setPositiveButton("start",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialogq, int which) {
Field field;
try {
field = dialogq.getClass().getSuperclass()
.getDeclaredField("mShowing");
field.setAccessible(true);
// 设置mShowing值,欺骗android系统
field.set(dialogq, false);//如果为true则会推出
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
dialog.setNegativeButton("stop", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();

}
});
dialog.setView(linear);

posted @ 2014-09-26 09:06  3H  阅读(2615)  评论(0编辑  收藏  举报