我对Android / Java比较陌生。感谢Stack Overflow,我从这里提出的问题中学到了很多东西。但是,我现在仍然坚持这个问题。
我在启动应用时会弹出密码输入AlertDialog
。它从EditText
读取密码,并将其与存储在文件中的密码进行比较。当需要提供无效/错误的密码时,我需要额外的AlertDialog
。这也是我实施的。
现在,第二个对话框有两个按钮 - 重置和重试。当我们点击 Retry 时,我希望活动再次显示第一个对话框。这是我无能为力的地方。如果有人能够提供有效的解决方案和一些解释,我将非常感激。
这是我的代码:
LayoutInflater li = LayoutInflater.from(context);
View passView = li.inflate(R.layout.authdialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(passView);
final EditText passInput = (EditText) passView.findViewById(
R.id.editTextDialogUserInput);
final TextView txtv = (TextView) findViewById(R.id.textv);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String data = "";
try {
FileInputStream fis = openFileInput("authfile");
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
data = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (data.toString().equals(
passInput.getText().toString())) {
txtv.setText("You Have Logged in");
} else {
LayoutInflater ln = LayoutInflater.from(context);
View invalidView = ln.inflate(R.layout.invdialog,
null);
AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(
context);
invalidDialogBuild.setView(invalidView);
// set dialog message
invalidDialogBuild
.setCancelable(false)
.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
})
.setNegativeButton(
"Reset",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.cancel();
AlertDialog invalidDialog = invalidDialogBuild
.create();
// show it
invalidDialog.show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
答案 0 :(得分:1)
创建两个单独的对话框方法,并按照我在下面的代码检查中的方式调用它们。
private void dialogShow() { LayoutInflater li = LayoutInflater.from(context); View passView = li.inflate(R.layout.authdialog, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); // set prompts.xml to alertdialog builder alertDialogBuilder.setView(passView); final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput); final TextView txtv = (TextView) findViewById(R.id.textv); // set dialog message alertDialogBuilder.setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String data = ""; try { FileInputStream fis = openFileInput("authfile"); InputStreamReader in = new InputStreamReader(fis); BufferedReader br = new BufferedReader(in); data = br.readLine(); } catch (Exception e) { e.printStackTrace(); } if (data.toString().equals(passInput.getText().toString())) { txtv.setText("You Have Logged in"); } else { RetryDialog();//Call the dialog of retry } } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); }
//Shows the Retry Dialog
private void RetryDialog()
{
LayoutInflater ln = LayoutInflater.from(context);
View invalidView = ln.inflate(R.layout.activity_main, null);
AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(context);
// invalidDialogBuild.setView(invalidView);
// set dialog message
invalidDialogBuild.setCancelable(false)
.setPositiveButton("Retry", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialogShow();
dialog.cancel();
}
}).setNegativeButton("Reset", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog invalidDialog = invalidDialogBuild.create();
// show it
invalidDialog.show();
}
<强>编辑:强>
使用ThreeButtons进行对话。
/** * This is method to display dialog with three * button("Yes, No and Cancel button") */ public void showThreeButtonDialog() { private AlertDialog m_alertDialog; private AlertDialog.Builder m_builder = new AlertDialog.Builder(this); m_builder.setTitle("Title Text"); m_builder.setMessage("Dialog Message"); m_builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface p_dialog, int p_which) { Toast.makeText(AlertDialogActivity.this, "Press Yes", Toast.LENGTH_SHORT).show(); } }); m_builder.setNeutralButton("No"), new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface p_dialog, int p_which) { Toast.makeText(AlertDialogActivity.this, "Press No", Toast.LENGTH_SHORT).show(); } }); m_builder.setNegativeButton("Cancel"), new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface p_dialog, int p_which) { Toast.makeText(AlertDialogActivity.this, "Press Cancel", Toast.LENGTH_SHORT).show(); } }); m_alertDialog = m_builder.create(); m_alertDialog.show(); }
答案 1 :(得分:1)
Grishu打败了我,我的至少更短......
private void showLoginDialog(final Context context) {
LayoutInflater li = LayoutInflater.from(context);
View passView = li.inflate(R.layout.authdialog, null);
final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput);
final TextView txtv = (TextView) findViewById(R.id.textv);
new AlertDialog.Builder(context)
.setView(passView)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String data = "";
try {
FileInputStream fis = openFileInput("authfile");
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
data = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (data.toString().equals(passInput.getText().toString())) {
txtv.setText("You Have Logged in");
} else {
dialog.dismiss();
showInvalidDialog(context);
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.show();
}
private void showInvalidDialog(final Context context) {
LayoutInflater ln = LayoutInflater.from(context);
View invalidView = ln.inflate(R.layout.invdialog, null);
new AlertDialog.Builder(context)
.setView(invalidView)
.setCancelable(false)
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
showLoginDialog(context);
}
})
.setNegativeButton("Reset", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.show();
}
说明:将两个对话框的代码移动到可以相互调用的单独方法中。随后使用构建器模式来保持代码简洁和优雅。除非您想要调用潜在的OnCancelListener,否则请使用dialog.dismiss()而不是dialog.cancel()。