I have setup a custom Preference that must launch a dialog with 3 TextViews to confirm and change a password.
I hope that the dialog will not be closed and display a prompt information if a user input error information and click "Save" button, how can I do? Thanks!
BTW, is it suitable to use DialogPreference control to deal with the business logic in 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>