Remember the password and the implementation of the Automatic Logon interface (5), remember the Password Logon Interface

Source: Internet
Author: User

Remember the password and the implementation of the Automatic Logon interface (5), remember the Password Logon Interface

You cannot control the weather, but it can change your mood. You cannot change your appearance, but you can master yourself. You cannot predict tomorrow, but you can cherish today.


This document introduces the implementation of remembering passwords and the automatic logon interface.


Example



The following figure shows the layout file res/layout/activity_login.xml: (logon Interface)

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ drawable/login"> <ImageButton android: id = "@ + id/img_btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: background = "@ drawable/quit"/> <TextView android: id = "@ + id/TV _zh" android: layout_width = "wrap_content" android: layout_height = "35dp" android: layout_marginLeft = "15dp" android: layout_marginTop = "12dp" android: gravity = "bottom" android: text = "account:" android: textColor = "#000000" android: textSize = "20sp"/> <EditText android: id = "@ + id/et_zh" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_below = "@ + id/TV _zh" android: layout_marginLeft = "15dp" android: layout_marginRight = "12dp"/> <TextView android: id = "@ + id/TV _pws" android: layout_width = "wrap_content" android: layout_height = "35dp" android: layout_below = "@ + id/et_zh" android: layout_marginLeft = "15dp" android: layout_marginTop = "12dp" android: gravity = "bottom" android: text = "Password:" android: textColor = "#000000" android: textSize = "20sp"/> <EditText android: id = "@ + id/et_pws" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_below = "@ + id/TV _pws" android: layout_marginLeft = "15dp" android: layout_marginRight = "12dp"/> <CheckBox android: id = "@ + id/cb_pws" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/et_pws" android: layout_marginLeft = "15dp" android: text = "Remember password" android: textColor = "#000000"/> <CheckBox android: id = "@ + id/cb_auto" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/cb_pws" android: layout_marginLeft = "15dp" android: text = "Automatic Logon" android: textColor = "#000000"/> <Button android: id = "@ + id/btn_login" android: layout_width = "80dp" android: layout_height = "40dp" android: layout_alignParentRight = "true" android: layout_alignTop = "@ + id/cb_auto" android: layout_below = "@ + id/et_pws" android: layout_marginRight = "10dp" android: gravity = "center" android: text = "login" android: textColor = "#000000" android: textSize = "20sp"/> </RelativeLayout>

The following figure shows the res/layout/activity_logo.xml layout file: (logon Buffer Interface)

<? 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 = "@ drawable/login" android: orientation = "vertical"> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_weight = "3"> <ProgressBar android: id = "@ + id/pgBar" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true"/> <TextView android: id = "@ + id/tv1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/pgBar" android: layout_centerHorizontal = "true" android: text = "logging on... "android: textColor =" #000000 "android: textSize =" 20sp "/> </RelativeLayout> <LinearLayout android: layout_width =" match_parent "android: layout_height =" wrap_content "android: layout_weight = "1" android: gravity = "center"> <Button android: id = "@ + id/btn_back" android: layout_width = "70dp" android: layout_height = "35dp" android: text = "cancel" android: textColor = "#000000" android: textSize = "15sp"/> </LinearLayout>

The following figure shows the res/layout/activity_welcome.xml layout file: (welcome page)

<? 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 = "@ drawable/bg" android: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "20dp" android: gravity = "center" android: text = "Login successful, go to the User Interface" android: textColor = "# ff00ff" android: textSize = "25sp"/> </LinearLayout>

The following is the LoginActivity. java interface file: (To register)

Public class LoginActivity extends Activity {private EditText userName, password; private CheckBox rem_pw, auto_login; private Button btn_login; private ImageButton btnQuit; private SharedPreferences sp; private String userNameValue, passwordValue; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title setContentView (R. layout. activity_login); initView (); initData ();} private void initView () {// obtain the Instance Object userName = (EditText) findViewById (R. id. et_zh); password = (EditText) findViewById (R. id. et_pws); rem_pw = (CheckBox) findViewById (R. id. cb_pws); auto_login = (CheckBox) findViewById (R. id. cb_auto); btn_login = (Button) findViewById (R. id. btn_login); btnQuit = (ImageButton) findViewById (R. id. img_btn); sp = this. getSharedPreferences ("userInfo", Context. MODE_WORLD_READABLE);} private void initData () {// identifies the status of the multiple-choice box for remembering passwords if (sp. getBoolean ("ISCHECK", false) {// The default setting is to record the password status rem_pw.setChecked (true); userName. setText (sp. getString ("USER_NAME", ""); password. setText (sp. getString ("PASSWORD", ""); // determines the status of the if (sp. getBoolean ("AUTO_ISCHECH", false) {// sets the default value to auto_login.setChecked (true); // The jump interface Intent intent = new Intent (LoginActivity. this, LogoActivity. class); LoginActivity. this. startActivity (intent) ;}}// the default logon listening event is jin password 1230btn_login.setOnClickListener (new OnClickListener () {public void onClick (View arg0) {userNameValue = userName. getText (). toString (); passwordValue = password. getText (). toString (); if (userNameValue. equals ("jin") & passwordValue. equals ("1230") {// Toast. makeText (LoginActivity. this, "Logon successful", Toast. LENGTH_SHORT ). show (); // log on successfully and remember the password box to save the user information if (rem_pw.isChecked () {// remember the user name and password Editor = sp. edit (); editor. putString ("USER_NAME", userNameValue); editor. putString ("PASSWORD", passwordValue); editor. commit ();} // jump interface Intent intent = new Intent (LoginActivity. this, LogoActivity. class); LoginActivity. this. startActivity (intent); // finish ();} else {Toast. makeText (LoginActivity. this, "the user name or password is incorrect. Please log on again", Toast. LENGTH_LONG ). show () ;}}); // listen to remember the password multiple select box button event rem_pw.setOnCheckedChangeListener (new OnCheckedChangeListener () {public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (rem_pw.isChecked () {Toast. makeText (LoginActivity. this, "Remember that the password has been selected", Toast. LENGTH_LONG ). show (); sp. edit (). putBoolean ("ISCHECK", true ). commit ();} else {Toast. makeText (LoginActivity. this, "Remember that the password is not selected", Toast. LENGTH_LONG ). show (); sp. edit (). putBoolean ("ISCHECK", false ). commit () ;}}); // listens to auto_login.setOnCheckedChangeListener (new OnCheckedChangeListener () {public void onCheckedChanged (CompoundButton arg0, boolean arg1) {if (auto_login.isChecked () {Toast. makeText (LoginActivity. this, "automatically logged on selected", Toast. LENGTH_LONG ). show (); sp. edit (). putBoolean ("AUTO_ISCHECK", true ). commit ();} else {Toast. makeText (LoginActivity. this, "Automatic Logon not selected", Toast. LENGTH_LONG ). show (); sp. edit (). putBoolean ("AUTO_ISCHECK", false ). commit () ;}}); // Delete event (exit program) btnQuit. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {finish ();}});}}

The following is the LogoActivity. java interface file: (To register)

Public class LogoActivity extends Activity implements Runnable {private ProgressBar progressBar; private Button backButton; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_logo); progressBar = (ProgressBar) findViewById (R. id. pgBar); backButton = (Button) findViewById (R. id. btn_back); progressBar. setMax (3000); // start a delay Thread new Thread (this ). start (); backButton. setOnClickListener (new OnClickListener () {public void onClick (View v) {finish () ;}}) ;}public void run () {try {Thread. sleep (2000); // startActivity (new Intent (this, WelcomeActivity. class);} catch (Exception e) {e. printStackTrace ();}}}

The following is the WelcomeActivity. java interface file:

public class WelcomeActivity extends Activity{protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);}}


Take your time and enjoy it. For the original code, pass by, and learned information, please leave a comment ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.