First, the goal.
Customize the Enter Password dialog box, judging by the password you enter and the password you have saved.
Second, the code implementation.
1, the tenth section of the method to customize a password to enter the dialog box.
2. Set the Click event for the button, mainly about the click event of the "Confirm" button.
①. Get the text through the TextView object's GetText () and convert it to a string via ToString () and remove the space by trimming () to get the input password of the string object;
②. Obtains the saved password through the Sharedpreferences object's GetString (string key, String defvalue), the parameter String key is the name of the key;
③. According to the input password and obtain the saved password to judge, and according to the situation after the decision to operate accordingly.
Enter the code for the Password dialog box:
1 protected voidShowinputpwddialog () {2Alertdialog.builder Builder =NewBuilder (homeactivity. This);3View view = View.inflate ( This, R.layout.inputpwddialog,NULL);4Inputpwddialog_et_pwd =(TextView) View5 . Findviewbyid (r.id.inputpwddialog_et_pwd);6inputpwddialog_bt_conf =(Button) View7 . Findviewbyid (r.id.inputpwddialog_bt_conf);8Inputpwddialog_bt_cancel =(Button) View9 . Findviewbyid (r.id.inputpwddialog_bt_cancel);Ten //Set click on "Cancel" to make the dialog disappear. OneInputpwddialog_bt_cancel.setonclicklistener (NewOnclicklistener () { A - @Override - Public voidOnClick (View v) { the Alertdialog.dismiss (); - } - }); - //set the Listen event for the OK button +Inputpwddialog_bt_conf.setonclicklistener (NewOnclicklistener () { - + @Override A Public voidOnClick (View v) { at //get the password in the input box -String Inputpassword =Inputpwddialog_et_pwd.gettext () - . toString (). Trim (); - //get the password stored in the config file -String Savedpassword = sp.getstring ("Password", "" "); - //complete related operations based on two password comparisons in if(Textutils.isempty (Inputpassword)) { -Toast.maketext (homeactivity. This, "Enter password is empty", 0). Show (); to}Else if(Inputpassword.equals (Savedpassword)) { + Alertdialog.dismiss (); -Toast.maketext (homeactivity. This, "Password entered correctly, go to Main page", 0). Show (); the}Else { *Toast.maketext (homeactivity. This, "Password input error", 0). Show (); $Inputpwddialog_et_pwd.settext ("");Panax Notoginseng return; - } the } + }); A Builder.setview (view); theAlertdialog =builder.show (); +}View Code
Android instance-Mobile security Defender (12)-Make Input Password dialog box and set corresponding Click event