如何从第二个警报对话框中导出按钮导航到上一个警告对话框?

时间:2016-01-07 05:50:56

标签: android dialog

所以我有这个警告对话框,有两个选项按钮。如果我单击“是”但输入错误,它将创建另一个对话框,提醒您输入正确的文本。

ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    kode_konfirmasi = userInput.getText().toString();
                    if(kode_konfirmasi.equals("lrsstwbsd")) {
                        strIdWaktuVaksin = ambil_notif_id_vaksin;
                        strWaktuDatang = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                        strStatusVaksin = "done";

                        update(strIdWaktuVaksin, strWaktuDatang, strStatusVaksin);

                        new LongOperation().execute(url);
                    }else{
                        AlertDialog.Builder abt = new AlertDialog.Builder(MainPetInformation.this);
                        abt.setTitle("sorry, wrong input. Try again");
                        abt.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                                return;
                            }
                        });
                        abt.show();
                    }

                }
            });

但是,我不知道如何让第二个对话框按钮导航到用户可以再次输入的上一个对话框

2 个答案:

答案 0 :(得分:1)

创建AlertDialog,如下所示 -

final AlertDialog ab = new AlertDialog.Builder(this)
            .setTitle("First")
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();

    ab.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = ab.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                    builder1.setTitle("second");
                    builder1.setPositiveButton("okay", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            abt.dismiss();

                        }
                    });

                    abt = builder1.create();
                    abt.show();

                    if(correctInput)
                        // ab.dismiss();
                }
            });
        }
    });
    ab.show();

创建AlertDialog abt作为类成员。

答案 1 :(得分:0)

首先,我建议您使用ToastLENGTH_LONG一起显示您的错误,因为它会在一段时间后自行处理,而您的AlertDialog仍然会在那里......

public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_LONG).show();
}

但是,如果您想使用Alertdialog制作您的AlertDialog对象全局变量,请在第二个对话框onClick中调用它。

AlertDialog _alert;

final AlertDialog.Builder _builder =new AlertDialog.Builder(MainActivity.this);
    _builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
             if(condition) {
               // ---- 
             }else{
                AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Wrong Input");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     _alert = _builder.create();
                     _alert.show();
                   //-- return to first dialog
                 }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
            });
    _alert = _builder.create();
    _alert.show()