如何在按钮单击按钮单击对话框窗口后等待

时间:2017-09-25 16:09:34

标签: java android

我有一个带有textView的小型userprofile表,例如[email],右边有一个[button]来更改地址。

当我点击按钮时,会打开一个带有EditText输入的小型DialogWindow,以便用户可以使用肯定按钮应用该值。

但是有一种恼人的效果我不知道如何解决。创建EditTextDialog后,我想要干燥输入值。 (见下面的代码) 但我的第一个按钮点击不等待对话框中的正面按钮点击。所以在输入输入之前会立即'空'烤。

Button btnChangeEmail = (Button) getView().findViewById(R.id.btn_settings_changeemail);
        btnChangeEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             MyDialogBuilder myDialogBuilder = new MyDialogBuilder(getString(R.string.changeemail), getActivity());
                myDialogBuilder.createEditTextInputDialog();
                myDialogBuilder.toastInput();
            }
        });

在按钮上我想将输入字符串存储在对象中。实际上我想要事后干杯或做一些其他的调用,比如把它发送到MySQL-DB。但我需要尽可能灵活,以便我可以称之为makeInputDialog().toastTheInput().andSendItToWeb()等等。但正如已经说过的那样......根本没有字符串,因为第一个按钮不等待第二个按钮。

我需要改变什么?在此先感谢。

这是我的对话框Builder类:

public class MyDialogBuilder {

  String caption;
  String input;
  Context mContext;

  public MyDialogBuilder(String caption, Context mContext) {
    this.caption = caption;
    this.mContext = mContext;
  }

  MyDialogBuilder setInputValue(String value) {
    this.input = value;
    return this;
  }

  MyDialogBuilder createEditTextInputDialog() {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View inputDialogView = inflater.inflate(R.layout.dialog_input_edittext, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(inputDialogView);

    final EditText etInput = (EditText) inputDialogView.findViewById(R.id.et_inputdialog);
    final TextView tvCaption = (TextView) inputDialogView.findViewById(R.id.tv_inputdialog_caption);

    tvCaption.setText(caption);

    builder.setCancelable(true)
      .setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          setInputValue(etInput.getText().toString());
        }
      }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.cancel();
        }
      });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();

    return this;

  }

  MyDialogBuilder toastInput() {
    Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
    return this;
  }

}

3 个答案:

答案 0 :(得分:3)

在构建器模式中,您只需构建情境,而不是立即执行。您应该稍后应用更改并进行检查:

public class MyDialogBuilder {

    String caption;
    String input;
    Context mContext;
    boolean showToast;

    public MyDialogBuilder(String caption, Context mContext) {
        this.caption = caption;
        this.mContext = mContext;
    }

    MyDialogBuilder setInputValue(String value) {
        this.input = value;
        return this;
    }

    MyDialogBuilder createEditTextInputDialog() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View inputDialogView = inflater.inflate(R.layout.dialog_input_edittext, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(inputDialogView);

        final EditText etInput = (EditText) inputDialogView.findViewById(R.id.et_inputdialog);
        final TextView tvCaption = (TextView) inputDialogView.findViewById(R.id.tv_inputdialog_caption);

        tvCaption.setText(caption);

        builder.setCancelable(true)
                .setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setInputValue(etInput.getText().toString());

                        // Toast here
                        if (showToast) {
                            Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
                        }
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

        return this;

    }

    MyDialogBuilder toastInput() {
        // Set here to show toast or not
        showToast = true;
        return this;
    }

}

所以在toastInput()中你明确表示应该显示一个toast,然后检查该boolean是否设置为true,如果是,则显示toast。

答案 1 :(得分:1)

MyDialogBuilder toastInput() {
    Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
    return this;
}   

用以下代码替换上面的代码。

MyDialogBuilder toastInput() {
    if(null != input){
        Toast.makeText(mContext, input, Toast.LENGTH_SHORT).show();
    }
    return this;
}

答案 2 :(得分:1)

你需要让正面按钮触发你的祝酒词:

MyDialogBuilder createEditTextInputDialog() {
...
builder.setCancelable(true)
  .setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      setInputValue(etInput.getText().toString());
      // check logic to show the toast
      showToast(etInput.getText().toString());
    }
  }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      dialog.cancel();
    }
  });

然后创建一个显示吐司的方法:

void showToast(String msg) {   
    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}

您可以使用现有的逻辑来检查是否应该显示吐司,但是这样可以确保只在单击肯定按钮时调用toast方法(或检查是否应显示吐司的方法)。

相关问题