在 Android 的 AlertDialog 的 EditText 中确认密码为空
Confirm password is empty in EditText in AlertDialog in Android
我的方法如下: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();
发生什么事了??我在这里错过了什么?我看不到那东西,它一定是某物 "stupid"。请问,有什么帮助吗?谢谢
使用 Editable.toString
其中 returns 具有与此序列相同顺序的相同字符的字符串。 如:
第一个选项:
if(password.equals(confirm.toString())){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
第二个选项:
从 password
获取 Editable
对象为:
Editable editablePassword = new SpannableStringBuilder(password);
if(editablePassword.equals(confirm)){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
试试这个。
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);
}
}
});
我的方法如下: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();
发生什么事了??我在这里错过了什么?我看不到那东西,它一定是某物 "stupid"。请问,有什么帮助吗?谢谢
使用 Editable.toString
其中 returns 具有与此序列相同顺序的相同字符的字符串。 如:
第一个选项:
if(password.equals(confirm.toString())){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
第二个选项:
从 password
获取 Editable
对象为:
Editable editablePassword = new SpannableStringBuilder(password);
if(editablePassword.equals(confirm)){
Log.d(DEBUG_TAG1,"passwords match ");
}
else{
....
}
试试这个。
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);
}
}
});