Last active
August 29, 2015 13:58
-
-
Save yamanetoshi/10235370 to your computer and use it in GitHub Desktop.
Custom DialogFragment in Preferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MyPasswordInputDialog extends DialogFragment { | |
private SharedPreferences mPref; | |
private EditText mPasswd1; | |
private EditText mPasswd2; | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
mPref = PreferenceManager.getDefaultSharedPreferences(MyPasswordInputDialog.this.getActivity()); | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
LayoutInflater factory = LayoutInflater.from(getActivity()); | |
final View inputView = factory.inflate(R.layout.password_input_layout, null); | |
mPasswd1 = (EditText)inputView.findViewById(R.id.passwd1); | |
mPasswd2 = (EditText)inputView.findViewById(R.id.passwd2); | |
builder.setView(inputView); | |
builder.setMessage(getResources().getString(R.string.password_input_tile)); | |
builder.setPositiveButton(getResources().getString(R.string.positive_button_string), | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int whichButton) { | |
// no implementation | |
} | |
}); | |
builder.setNegativeButton(getResources().getString(R.string.negative_button_string), | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
// no implementation | |
} | |
}); | |
builder.setCancelable(true); | |
AlertDialog dialog = builder.create(); | |
dialog.setOnShowListener(new OnShowListener() { | |
@Override | |
public void onShow(DialogInterface dialog) { | |
final DialogInterface d = dialog; | |
Button buttonOK = ((AlertDialog)dialog).getButton( DialogInterface.BUTTON_POSITIVE ); | |
buttonOK.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
if (passwdInputCheck()) { | |
d.dismiss(); | |
} | |
} | |
}); | |
} | |
}); | |
return dialog; | |
} | |
private boolean passwdInputCheck() { | |
String passwd1 = mPasswd1.getText().toString(); | |
String passwd2 = mPasswd2.getText().toString(); | |
if (passwd1.equals("") { | |
Toast.makeText(getActivity(), | |
getActivity().getResources().getString(R.string.toast_password_column_error), | |
Toast.LENGTH_LONG).show(); | |
return false; | |
} | |
if (!passwd1.equals(passwd2)) { | |
Toast.makeText(getActivity(), | |
getActivity().getResources().getString(R.string.toast_password_input_error), | |
Toast.LENGTH_LONG).show(); | |
return false; | |
} | |
Editor e = mPref.edit(); | |
e.putString("password", passwd1); | |
e.commit(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment