MD5 simple instance, md5 instance
When you click the button, you will first determine whether to log on for the first time. If you log on for the first time, a pop-up window for password setting will pop up. If you log on, a pop-up window will pop up.
The entered password is encrypted with MD5.
Package com.org. demo. wangfeng. demo; import com.org. wangfeng. r; import android. app. activity; import android. app. alertDialog; import android. content. sharedPreferences; import android. OS. bundle; import android. text. textUtils; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class HomeActivity extends Activity {private Button bbButton; pr Ivate SharedPreferences mPref; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. homeactivity); mPref = getSharedPreferences ("config", MODE_PRIVATE); bbButton = (Button) findViewById (R. id. bb_home); bbButton. setOnClickListener (new OnclickItem ();} private class OnclickItem implements View. on ClickListener {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub String savedPassword = mPref. getString ("password", null); if (! TextUtils. isEmpty (savedPassword) {// enter the password pop-up window showPasswordInputDialog ();} else {// if no password is set, the pop-up window showPasswordSetDialog ();}}} /*** pop-up window for setting the password */private void showPasswordSetDialog () {AlertDialog. builder builder = new AlertDialog. builder (this); final AlertDialog dialog = builder. create (); View view = View. inflate (this, R. layout. dialog_set_password, null); dialog. setView (view, 0, 0, 0, 0); // set the margin to 0., Ensure that in 2. final EditText edPassWord = (EditText) view. findViewById (R. id. et_password); final EditText edPassWordConfirm = (EditText) view. findViewById (R. id. et_password_confirm); Button btnOK = (Button) view. findViewById (R. id. btn_ OK); Button btnCancel = (Button) view. findViewById (R. id. btn_cancel); btnOK. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub String password = edPassWord. getText (). toString (); String passwordConfirm = edPassWordConfirm. getText (). toString (); if (! TextUtils. isEmpty (password )&&! PasswordConfirm. isEmpty () {// when the two inputs are the same if (password. equals (passwordConfirm) {Toast. makeText (HomeActivity. this, "Login successful", Toast. LENGTH_SHORT ). show (); // Save the password as mPref. edit (). putString ("password", MD5Utils. encode (password )). commit (); dialog. dismiss ();} else {Toast. makeText (HomeActivity. this, "2 inconsistent passwords", Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (HomeActivity. this, "the input content cannot be blank", Toast. LENGTH_SHORT ). show () ;}}); btnCancel. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub dialog. dismiss () ;}}); dialog. show ();}/*** enter password pop-up window */private void showPasswordInputDialog () {AlertDialog. builder builder = new AlertDialog. builder (this); final AlertDialog dialog = builder. create (); View view = View. inflate (this, R. layout. dialog_input_password, null); dialog. setView (view, 0, 0, 0, 0); final EditText etPassword = (EditText) view. findViewById (R. id. et_password); Button btnOk = (Button) view. findViewById (R. id. btn_ OK); Button btnCancel = (Button) view. findViewById (R. id. btn_cancel); btnOk. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method s Tub String password = etPassword. getText (). toString (); if (! TextUtils. isEmpty (password) {String savedPassword = mPref. getString ("password", null); if (MD5Utils. encode (password ). equals (savedPassword) {Toast. makeText (HomeActivity. this, "Login successful", Toast. LENGTH_SHORT ). show (); dialog. dismiss ();} else {Toast. makeText (HomeActivity. this, "Incorrect password", Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (HomeActivity. this, "the input content cannot be blank", Toast. LENGTH_SHORT ). show () ;}}); btnCancel. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub dialog. dismiss () ;}}); dialog. show ();}}
Package com.org. demo. wangfeng. demo; import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class MD5Utils {/*** md5 encryption ** @ param password * @ return */public static String encode (String password) {try {MessageDigest instance = MessageDigest. getInstance ("MD5"); // obtain the MD5 Algorithm object byte [] digest = instance. digest (password. getBytes (); // string encryption, returns the byte array StringBuffer sb = new StringBuffer (); for (byte B: digest) {int I = B & 0xff; // get the low-byte eight-bit valid value String hexString = Integer. toHexString (I); // convert the integer to hexadecimal if (hexString. length () <2) {hexString = "0" + hexString; // if it is 1 bit, add 0} sb. append (hexString);} return sb. toString ();} catch (NoSuchAlgorithmException e) {e. printStackTrace (); // if this algorithm does not exist, an exception is thrown and the return "" ;}} is not returned here "";}}<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "# fff" android: orientation = "vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "#66ff6600" android: gravity = "center" android: padding = "10dp" android: text = "set password" android: textColor = "@ android: color/black "android: textSize =" 20sp "/> <EditText android: id =" @ + id/et_password "android: layout_width =" match_parent "android: layout_height = "wrap_content" android: hint = "enter the password" android: inputType = "textPassword"> </EditText> <EditText android: id = "@ + id/et_password_confirm" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Please enter the password again" android: inputType = "textPassword"> </EditText> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <Button android: id = "@ + id/btn_ OK" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "OK"/> <Button android: id = "@ + id/btn_cancel" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "cancel"/> </LinearLayout>View Code
In android, if (text = null | text. length () = 0) is encapsulated.
In android. text. TextUtils
Public static boolean isEmpty (CharSequence str) {if (str = null | str. length () = 0) return true; else return false;} so we can use
TextUtils.isEmpty(text)
Replace
if(text == null || text.length() == 0)