我已经设置了一个自定义首选项,必须启动一个包含3个TextViews的对话框来确认和更改密码。
我希望对话框不会关闭并显示提示信息,如果用户输入错误信息并单击“保存”按钮,我该怎么办?谢谢!
BTW,是否适合使用DialogPreference控件来处理PreferenceScreen中的业务逻辑?public class DialogChangePassword extends DialogPreference {
private String strPass1;
private String strPass2;
public DialogChangePassword(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.unlock_custom_dialog);
}
private View view;
@Override
protected View onCreateDialogView() {
// TODO Auto-generated method stub
view=super.onCreateDialogView();
return view;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
final EditText password1 = (EditText) view.findViewById(R.id.EditText_Pwd1);
final EditText password2 = (EditText)view.findViewById(R.id.EditText_Pwd2);
strPass1 = password1.getText().toString();
strPass2 = password2.getText().toString();
if (strPass1.equals(strPass2)) {
SavePassword();
super.onDialogClosed(positiveResult);
} else {
Toast.makeText(view.getContext(),"Input Error",Toast.LENGTH_LONG).show();
//Prevent to close the dialog! How to do?
}
}
}
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="@string/PreferenceSummary"
android:title="@string/Preference" >
<ui.custom.DialogChangePassword
android:key="prefKeyResetQuests"
android:dialogIcon="@android:drawable/ic_dialog_alert"
android:title="Reset Password"
android:summary="Reset Password"
android:dialogMessage="Reset Password"
android:positiveButtonText="Save"
android:negativeButtonText="Cancel"/>
/>
</PreferenceScreen>
unlock_custom_dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/TextView_Pwd1"
android:text="string/settings_oldpassword"
android:textStyle="bold" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText_OldPwd" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/TextView_Pwd11"
android:text="string/settings_password"
android:textStyle="bold" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText_Pwd1"
android:inputType="textPassword" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/TextView_Pwd2"
android:text="string/settings_password2"
android:textStyle="bold" />
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText_Pwd2"
android:inputType="textPassword" />
</LinearLayout>
答案 0 :(得分:1)
在DialogPreference
:
@Override
public void onClick(DialogInterface dialog, int which)
{
// If the user clicked "Ok" but the "savePassword" function returns false (mainly because the checks weren't good), we block the closing of the Dialog
if (which == DialogInterface.BUTTON_POSITIVE && !savePassword())
{
try
{
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, false);
}
catch (NoSuchFieldException | IllegalAccessException e)
{
e.printStackTrace();
}
}
// else, we remove the block if it was put before
else
{
try
{
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialog, true);
}
catch (NoSuchFieldException | IllegalAccessException e)
{
e.printStackTrace();
}
}
}
这样,如果用户想要在验证失败之前或之后离开对话框,他仍然可以。但如果输入错误并且用户单击“确定”,窗口将不会关闭。
PS:如果你想扩展DialogPreference
,你需要这个:
@Override
protected void onDialogClosed(boolean exitedViaOk)
{
super.onDialogClosed(exitedViaOk);
// Save only if the user clicked the "Ok" button
if (exitedViaOk)
{
savePassword();
}
}