动态更改警报对话框中的按钮文本 [英] Dynamically change button text in alert dialog

查看:55
本文介绍了动态更改警报对话框中的按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经弄混了几个小时,但似乎找不到如何获得它.基本上,我有一个用于创建密码的警报对话框.有一个密码字段和一个确认密码字段.侦听器检查它们是否匹配,并更新textView.我想做的是,当两个字段匹配时,将按钮上的文本从取消"更改为继续".一切都在进行中...

I've been messing with this for a few hours now, and just can't seem to find how to get it. Basically, I have an alert dialog for creating a password. There is a password field and a confirm password field. The listener checks if they match, and updates a textView. What I would like to do is change the text on the button from CANCEL to PROCEED when the two fields match. Everything is working up to that point...

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Build the dialog and set up the button click handlers
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Add variables
    final View layout = inflater.inflate(R.layout.fragment_setpassword, null);
    final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
    final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
    final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);

    builder.setView(layout)
    .setTitle(R.string.hdrSetPassword)
    .setMessage(R.string.hdrSetPassword)
    .setPositiveButton(R.string.hdrSetPassword, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Send the positive button event back to the host activity
            String strPassword1 = password1.getText().toString();
            String strPassword2 = password2.getText().toString();
            if (strPassword1.equals(strPassword2)) {
                Register.password = password1.getText().toString();
                mListener.onDialogPositiveClick(CreatePasswordFragment.this);
                dialog.dismiss();
            } else{
                // Set password to empty so it fails checks
                Register.password = "";
                mListener.onDialogNegativeClick(CreatePasswordFragment.this);
                dialog.dismiss();
            }
        }
    });
    password2.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
              String strPass1 = password1.getText().toString();
              String strPass2 = password2.getText().toString();
              if (strPass1.equals(strPass2)) {
                 // CHANGE BUTTON TEXT TO "PROCEED" HERE
                 error.setText(R.string.txtPasswordsMatch);
              } else {
                // CHANGE BUTTON TEXT TO "CANCEL" HERE
                 error.setText(R.string.txtPasswordsDontMatch);
              }
        }            
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
     });

    return builder.create();
}

推荐答案

您尝试过

Dialog dialog = builder.create();

afterTextChanged();

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(yourString);

?

public class MenuFragment extends DialogFragment {

    private static final String VALID = "OK";
    private static final String INVALID = "Not OK";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final View layout = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
        final EditText et1 = (EditText) layout.findViewById(R.id.et1);
        final EditText et2 = (EditText) layout.findViewById(R.id.et2);

        builder.setView(layout)
        .setTitle(R.string.hello_world)
        .setMessage(R.string.hello_world)
        .setPositiveButton(INVALID, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Send the positive button event back to the host activity
                String strPassword1 = et1.getText().toString();
                String strPassword2 = et2.getText().toString();
                if (strPassword1.equals(strPassword2)) {
                    dialog.dismiss();
                } else{
                    dialog.dismiss();
                }
            }
        });

        final AlertDialog dialog = builder.create();

        TextWatcher watcher = new TextWatcher() {
            public void afterTextChanged(Editable s) {
                  String strPass1 = et1.getText().toString();
                  String strPass2 = et2.getText().toString();
                  if (strPass1.equals(strPass2)) {

                      dialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(VALID);

                  } else {

                      dialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(INVALID);

                  }
            }            
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
         };


        et1.addTextChangedListener(watcher);
        et2.addTextChangedListener(watcher);

        return dialog;

    }

}

这篇关于动态更改警报对话框中的按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆