我的目标是在用户导航回我的应用程序时显示模式对话框。此模式对话框要求他们提供他们定义的 6 位 PIN。我是 Java GUI 和 Android 开发的新手,所以我的问题是.....
我发现如果用户选择在显示输入对话框时最小化应用程序,如果他们返回到应用程序,则对话框会相互堆叠。让他们在输入 PIN 期间最小化并返回应用程序的次数尽可能多地输入他们的 PIN。
/**
* On restart called when the app is being restarted on the screen
*/
@Override
public void onRestart() {
super.onRestart();
// must prompt with modal dialog for pin
final Dialog dialog = new Dialog(this);
dialog.setCancelable(false);
// check pin
dialog.setCanceledOnTouchOutside(false);
// set view to enterpin XML screen
dialog.setContentView(R.layout.enterpin);
// show dialog
if (!dialog.isShowing()) {
dialog.show();
}
// listen for button being clicked
Button button = (Button) dialog.findViewById(R.id.pinlogin);
button.setOnClickListener(new OnClickListener() {// anonymous inner
// class
// implementation
@Override
public void onClick(View v) {
EditText editText = (EditText) dialog
.findViewById(R.id.enterpintext);
try {
int enteredPin = Integer.parseInt(editText.getText()
.toString());
SharedPreferences sharedP = Prefs.get(WebViewActivity.this);
int temp = sharedP.getInt("pin", 0);
if (enteredPin == temp) {
pinCheck = true;
dialog.dismiss();
} else {
pinCheck = false;
dialog.setTitle("Incorrect Pin");
}
} catch (NumberFormatException e) {
Dialog dialog2 = new Dialog(WebViewActivity.this);
dialog2.setTitle("Enter Numbers Only");
dialog2.setCanceledOnTouchOutside(true);
dialog2.show();
}
}
});
}
有没有什么地方可以移动我的对话框初始化而不会觉得这是不好的编程习惯?我理解为什么我尝试的 dialog.isShowing() 方法不起作用,因为我的 Dialog 实例仅在该方法的生命周期内存在。
我还注意到,如果您将手机从垂直方向转到水平方向 90 度,反之,我的对话框就会消失。有人可以指出发生这种强制重绘时调用的方法链,以便我可以重绘对话框吗?