以下是我的方法1,输入新密码,2,确认密码。
public void editPassword(){
AlertDialog.Builder d = new AlertDialog.Builder(this);
d.setTitle("New password");
// Set up the input
final EditText newPassword = new EditText(this);
// Specify the type of input expected as a password
newPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
d.setView(newPassword);
Log.d(DEBUG_TAG1,"new password: " + newPassword.getText().toString());
final Editable changedPassword = newPassword.getText();
// Set up the buttons
d.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
confirmPassword(changedPassword);
}
});
d.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
d.show();
}
public void confirmPassword(final Editable password){
AlertDialog.Builder d = new AlertDialog.Builder(this);
d.setTitle("Confirm password");
// Set up the input
final EditText confirmPassword = new EditText(this);
// Specify the type of input expected as a password
confirmPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
d.setView(confirmPassword);
Log.d(DEBUG_TAG1,"Password: " + password.toString());
Log.d(DEBUG_TAG1,"Confirm password: " + confirmPassword.getText().toString());
final Editable confirm = confirmPassword.getText();
// Set up the buttons
d.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(password.equals(confirm)){
Log.d(DEBUG_TAG1,"passwords match ");
//TODO send new password to database
}
else{
//TODO dialog/toast saying "passwords don't match"
Log.d(DEBUG_TAG1,"passwords don't match ");
confirmPassword(password);
}
}
});
d.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
d.show();
}
在第一种方法editPassword()
中,密码已正确存储在变量changedPassword
中,并且正确传递给confirmPassword(changedPassword)
。
但是在方法confirmPassword()
中,变量confirm
final Editable confirm = confirmPassword.getText();
为空
发生了什么事?我在这里失踪了什么?我看不到它的东西,它必须是“愚蠢的”。请帮忙吗? THX
答案 0 :(得分:1)
使用Editable.toString
返回一个字符串,该字符串的顺序与此顺序相同。如:
第一个选项:
if(password.equals(confirm.toString())){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
第二个选项:
从Editable
获取password
对象:
Editable editablePassword = new SpannableStringBuilder(password);
if(editablePassword.equals(confirm)){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
答案 1 :(得分:1)
试试这个。
d.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Editable confirm = confirmPassword.getText();
if(password.equals(confirm)){
Log.d(DEBUG_TAG1,"passwords match ");
//TODO send new password to database
}
else{
//TODO dialog/toast saying "passwords don't match"
Log.d(DEBUG_TAG1,"passwords don't match ");
confirmPassword(password);
}
}
});