gpt4 book ai didi

android - 动态更改警报对话框中的按钮文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:46 24 4
gpt4 key购买 nike

我已经弄乱这个几个小时了,似乎找不到如何获得它。基本上,我有一个用于创建密码的警告对话框。有一个密码字段和一个确认密码字段。监听器检查它们是否匹配,并更新 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();
}

最佳答案

你试过吗

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;

}

}

关于android - 动态更改警报对话框中的按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com