【问题标题】:How to show AlertDialog Back To Back?如何显示 AlertDialog 背靠背?
【发布时间】:2016-02-15 11:42:45
【问题描述】:

这里我必须显示两个 AlertDialogs。单击第一个 AlertDialog 的正按钮后,将显示第二个 AlertDialog。

MainActivity.class

public class MainActivity extends AppCompatActivity {

public static final String ACTION_NEXT = "Next";
public static final String ACTION_DONE = "Done";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupSecretFields(this, "Configuration", "Please Enter Password", ACTION_NEXT);
}

private void setupSecretFields(final Context context, final String title, final String hint, final String button) {
    final AppCompatEditText appCompatEditText = new AppCompatEditText(context);
    appCompatEditText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    appCompatEditText.setHint(hint);
    appCompatEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

    final LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.addView(appCompatEditText);
    linearLayout.setPadding(40, 30, 40, 20);
    linearLayout.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT));

    final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
            .setPositiveButton(button, null)
            .setNegativeButton("Cancel", null)
            .setView(linearLayout)
            .setCancelable(false)
            .create();
    builder.show();

    builder.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (button.equals(ACTION_NEXT)) {
                if (appCompatEditText.getText().toString().trim().equals("1234")) {
                    builder.dismiss();
                    //Showing another AlertDialog
                    setupSecretFields(context, "Configuration", "Please Enter Api Url", ACTION_DONE);
                } else {
                    appCompatEditText.setError("Incorrect Password! Please try again");
                }
            } else {
                Toast.makeText(context, "Application Will Restart...", Toast.LENGTH_LONG).show();
            }
        }
    });
  }
}

第一个对话框工作正常,但没有显示另一个 AlertDialog。不知道有什么问题。请帮忙。

【问题讨论】:

  • 第二个对话框有错误吗?
  • 你试过builder.setPositiveButton()方法了吗?
  • @Vivek Mishra 没有错误.. 只是沉默..
  • @Sash_KP 我不能使用 builder.setPositiveButton() 因为它会在点击正/负按钮后关闭对话框。
  • 尝试设置 DialogInterface.BUTTON_POSITIVE 而不是 AlertDialog.BUTTON_POSITIVE。这行得通吗?

标签: android android-alertdialog


【解决方案1】:

变化:

final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
        .setPositiveButton(button, null)
        .setNegativeButton("Cancel", null)
        .setView(linearLayout)
        .setCancelable(false)
        .create();
builder.show();

到:

final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
        .setPositiveButton("Button", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            //DO YOUR TASK (open your second dialog)
            }
        })
        .setNegativeButton("Cancel", null)
        .setView(linearLayout)
        .setCancelable(false)
        .create();
builder.show();

应该帮助你。

【讨论】:

  • 谢谢先生,但是这个 OnClickListener 将在点击后关闭对话框。如果用户输入了错误的密码,我不想关闭它。在此处查看@eric 的答案.. stackoverflow.com/questions/2620444/…
猜你喜欢
  • 1970-01-01
  • 2013-10-19
  • 2010-12-02
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多