Log on to the instance and save the data (use SharedPreferences to save the user name for MOOC notes ).
Take notes when you perform operations after learning the video.
0. video address: http://www.imooc.com/video/3265
1. Function preview:
Note: 1) enter the wrong user name and password and click log on. A prompt box is displayed, indicating "Logon prohibited ";
2) enter the correct user name and password, and click log on. The prompt box "Logon successful" is displayed ";
3) enter the correct user name and password, check "Save User Name", and click "Log On". The pop-up window displays "Logon successful". Exit the APP and open the APP again. The user name already exists.
2. Specific layout:
Activity_main.xml:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <TextView android: id =" @ + id/textView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: layout_marginTop = "16dp" android: text = "username:"/> <EditText android: id = "@ + id/etuserName" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: layout_alignTop = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10"/> <TextView android: id = "@ + id/aa" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_below = "@ + id/etuserName" android: text = "password"/> <EditText android: id = "@ + id/etuserPass" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/etuserName" android: layout_alignParentRight = "true" android: layout_alignTop = "@ + id/aa" android: EMS = "10"> <requestFocus/> </EditText> <Button android: id = "@ + id/btnLogin" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_below = "@ + id/etuserPass" android: layout_marginTop = "62dp" android: onClick = "doClick" android: text = "login"/> <Button android: id = "@ + id/btnCancel" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/btnLogin" android: layout_alignBottom = "@ + id/btnLogin" android: layout_toRightOf = "@ + id/btnLogin" android: onClick = "doClick" android: text = "cancel"/> <CheckBox android: id = "@ + id/checkBox1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignRight = "@ + id/btnCancel" android: layout_below = "@ + id/etuserPass" android: layout_marginTop = "15dp" android: checked = "false" android: text = "Save User Name"/> </RelativeLayout>
View Code
3. MainActivity. java:
Public class MainActivity extends Activity {EditText etUserName, etUserPass; CheckBox chk; SharedPreferences pref; Editor editor; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // bind the etUserName = (EditText) findViewById (R. id. etuserName); etUserPass = (EditText) findViewById (R. id. etuserPass); chk = (CheckBox) findViewById (R. id. checkBox1); pref = getSharedPreferences ("UserInfo", MODE_PRIVATE); editor = pref. edit (); String name = pref. getString ("userName", ""); if (name = null) {chk. setChecked (false);} else {chk. setChecked (true); etUserName. setText (name) ;}/// add Response public void doClick (View v) {switch (v. getId () {case R. id. btnLogin: // convert it into a String to judge String name = etUserName. getText (). toString (). trim (); String pass = etUserPass. getText (). toString (). trim (); if ("admin ". equals (name) & "123456 ". equals (pass) {if (chk. isChecked () {// the user name and password are correct and the user name confirmation box is selected. // Save the data and submit it to the database editor. putString ("userName", name); editor. commit ();} else {editor. remove ("userName"); editor. commit ();} // Add a prompt box Toast. makeText (MainActivity. this, "Logon successful", Toast. LENGTH_LONG ). show ();} else {Toast. makeText (MainActivity. this, "Login prohibited", Toast. LENGTH_LONG ). show () ;}break; default: break ;}}}