gpt4 book ai didi

java - 显示多个警报对话框

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:03 28 4
gpt4 key购买 nike

我对 Android/Java 比较陌生。感谢 Stack Overflow,我能够从这里提出的问题中学到很多东西。但是,我现在陷入了这个问题。

我有一个密码输入AlertDialog,它会在我们启动应用程序时弹出。它从 EditText 中读取密码并将其与存储在文件中的密码进行比较。我需要一个额外的 AlertDialog,它会在提供无效/错误密码时显示。这也是我实现的。

现在,第二个对话框有两个按钮 - ResetRetry。当我们点击 Retry 时,我希望 Activity 再次显示第一个对话框。这是我无能为力的地方。如果有人能提供可行的解决方案和一点解释,我将非常感激。

这是我的代码:

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();

最佳答案

为您的对话框创建两个单独的方法并调用它们,就像我在下面的代码检查中所做的那样。

 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();
}

关于java - 显示多个警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239493/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com