final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.new_name, null);
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setView(view).setTitle(R.string.CaseName);
final EditText ed_name = view.findViewById(R.id.ed_name);
adb.setPositiveButton(R.string.Confirm,new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
if(...){ // 检测条件如果不符合,就保持弹框打开
try {//下面三句保持弹框打开
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
adb.setNegativeButton(R.string.Cancel,new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
try { // 下面三句关闭弹框
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);//true表示要关闭
} catch (Exception e) {
e.printStackTrace();
}
}})
.create().show();
【Android】AlertDialog.Builder使用
最新推荐文章于 2021-06-03 05:40:06 发布