Back to project page trivial-password.
The source code is released under:
MIT License
If you think the Android project trivial-password listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.hbabcock.trivialpassword; /*ww w.j a v a 2s . co m*/ import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; public class PasswordChangeFragment extends DialogFragment { private static final String TAG = "PasswordChangeFragment"; private AlertDialog mAlertDialog; private Button mButtonYes; private EditText mConfirmPassword; private EditText mCurrentPassword; private EditText mNewPassword; private PasswordManager mPasswordManager; private View mView; @Override public Dialog onCreateDialog(Bundle savedInstanceState){ mView = (View)getActivity().getLayoutInflater().inflate(R.layout.dialog_password_change, null); mPasswordManager = PasswordManager.get(getActivity()); ((EditText)mView.findViewById(R.id.password_hint)).setText(mPasswordManager.getHint()); mCurrentPassword = (EditText)mView.findViewById(R.id.current_password); mNewPassword = (EditText)mView.findViewById(R.id.new_password); mConfirmPassword = (EditText)mView.findViewById(R.id.confirm_password); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(mView); builder.setTitle(R.string.password_change); builder.setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mPasswordManager.savePassword(((EditText)mView.findViewById(R.id.confirm_password)).getText().toString(), ((EditText)mView.findViewById(R.id.password_hint)).getText().toString()); } }); mAlertDialog = builder.create(); return mAlertDialog; } @Override public void onResume(){ super.onResume(); mButtonYes = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE); updateDisplay(); mCurrentPassword.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s){ updateDisplay(); } public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count){} }); mNewPassword.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s){ updateDisplay(); } public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count){} }); mConfirmPassword.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s){ updateDisplay(); } public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count){} }); } private void updateDisplay(){ if (mConfirmPassword.getText().toString().equals(mNewPassword.getText().toString())){ mNewPassword.setTextColor(Color.rgb(0, 100, 0)); mConfirmPassword.setTextColor(Color.rgb(0, 100, 0)); if (mPasswordManager.isPassword(mCurrentPassword.getText().toString())){ mButtonYes.setEnabled(true); } else{ mButtonYes.setEnabled(false); } } else{ mNewPassword.setTextColor(Color.rgb(150, 0, 0)); mConfirmPassword.setTextColor(Color.rgb(150, 0, 0)); mButtonYes.setEnabled(false); } } }