5

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

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

现在,第二个对话框有两个按钮 -ResetRetry。我希望活动在我们单击时再次显示第一个对话框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();
4

2 回答 2

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();
}
于 2013-03-06T05:37:47.837 回答
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()。

于 2013-03-06T05:48:55.877 回答