我正在尝试创建一个包含2个edittexts的对话框来创建密码。这是我的代码:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setCancelable(false);
alert.setTitle(title);
alert.setMessage(message);
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.create_pass, null);
final EditText password = (EditText) layout.findViewById(R.id.password);
final EditText passconfirm = (EditText) layout.findViewById(R.id.repeat_password);
password.setMinEms(6);
alert.setView(layout);
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (!password.getText().toString().equals(passconfirm.getText().toString())) {
passconfirm.setError(getString(R.string.password_mismatch));
return;
} else if (password.getText().toString().isEmpty()) {
password.setError(getString(R.string.password_empty));
return;
} else {
secprefs.putString("master_password", password.getText().toString());
}
}
});
alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
对话框工作正常,如果遵守2个条件,则存储密码。 现在,如果不满足2个条件,则对话框将自行关闭。 我想使对话框关闭并显示小弹出错误。谁知道为什么?
我想设置的另外一件事是密码的最小长度。我尝试使用setMinEms(6),但它无法正常工作。
由于
答案 0 :(得分:2)
嗯,这就是我为进一步相关问题解决的问题:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setCancelable(false);
alert.setTitle(getString(R.string.dlgtitle));
alert.setMessage(getString(R.string.dlgmessage));
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.pass_create, null);
final EditText password = (EditText) layout.findViewById(R.id.password);
final EditText passconfirm = (EditText) layout.findViewById(R.id.repeat_password);
password.setMinEms(6);
alert.setView(layout);
alert.setPositiveButton(R.string.ok, null);
alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
final AlertDialog dialog = alert.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
if (ok != null) {
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!password.getText().toString().equals(passconfirm.getText().toString())) {
password.setError(getString(R.string.password_mismatch));
} else if (password.getText().toString().length() <= 6) {
password.setError(getString(R.string.pass_lenght));
} else if (password.getText().toString().isEmpty()) {
password.setError(getString(R.string.password_empty));
} else {
secprefs.putBoolean("first_step", false);
secprefs.putString("master_password", password.getText().toString());
dialog.cancel();
}
}
});
}
}
});
dialog.show();
答案 1 :(得分:0)
可以使用这样的模式检查密码的最小长度...示例
password.getText().toString().matches("[a-zA-Z0-9_]{6,}")
关于在输入中显示错误消息,您可以像这个示例一样使用Toast ......
Toast.makeText(this, "Input error!!!", Toast.LENGTH_LONG).show();