在警报对话框中动态更改按钮文本

时间:2014-03-16 11:12:14

标签: android

我现在已经搞乱了几个小时,似乎无法找到如何获得它。基本上,我有一个用于创建密码的警报对话框。有密码字段和确认密码字段。侦听器检查它们是否匹配,并更新textView。我想要做的是当两个字段匹配时将按钮上的文本从CANCEL更改为PROCEED。一切都在努力......

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();
}

1 个答案:

答案 0 :(得分:7)

你试过吗

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;

    }

}