SharedPreferences

(一)

1.效果图:实现用户名为张三密码为123则登陆成功,其他则登陆失败,并且可以记住密码

    

2.activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="com.example.administrator.hello11.MainActivity">
 8 
 9 
10 
11     <LinearLayout
12     android:orientation="horizontal"
13     android:layout_width="match_parent"
14     android:layout_height="wrap_content">
15     <TextView
16         android:id="@+id/tv"
17         android:text="用户名:"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content" />
20     <EditText
21         android:id="@+id/name"
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content" />
24 </LinearLayout>
25     <LinearLayout
26         android:orientation="horizontal"
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content">
29         <TextView
30             android:id="@+id/tv2"
31             android:text="密  码:"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content" />
34         <EditText
35             android:id="@+id/pass"
36             android:layout_width="match_parent"
37             android:layout_height="wrap_content" />
38     </LinearLayout>
39     <CheckBox
40         android:id="@+id/cb"
41         android:text="记住密码"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content" />
44     <LinearLayout
45         android:orientation="horizontal"
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content"
48         android:layout_gravity="center_horizontal">
49         <Button
50             android:text="登陆"
51             android:onClick="doClick"
52             android:id="@+id/btn_login"
53             android:layout_width="wrap_content"
54             android:layout_height="wrap_content" />
55         <Button
56             android:text="重置"
57             android:onClick="doClick"
58             android:id="@+id/btn_reset"
59             android:layout_width="wrap_content"
60             android:layout_height="wrap_content" />
61     </LinearLayout>
62 
63 </LinearLayout>

3.MainActivity.java

 1 package com.example.administrator.hello11;
 2 
 3 import android.content.SharedPreferences;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.CheckBox;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 
12 public class MainActivity extends AppCompatActivity {
13     private EditText name;
14     private EditText password;
15     private CheckBox checkBox;
16     private SharedPreferences.Editor editor;
17 
18 
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24 
25         name = (EditText)findViewById(R.id.name);
26         password =(EditText)findViewById(R.id.pass);
27         checkBox=(CheckBox)findViewById(R.id.cb);
28 
29         //获取sp对象
30         SharedPreferences sp = getSharedPreferences("userInfo",MainActivity.MODE_PRIVATE);
31         // 获取editor 对象
32         editor = sp.edit();
33 
34         //二次登陆
35         String user_name=sp.getString("u_name","");
36         String user_pass=sp.getString("u_pass","");
37         name.setText(user_name);
38         if (user_name==null||user_pass.length()==0){
39             checkBox.setChecked(false);
40         }else {
41             checkBox.setChecked(true);
42             password.setText(user_pass);
43         }
44 
45 
46     }
47     public  void doClick(View v){
48         switch (v.getId()){
49             case  R.id.btn_login:
50                 String username = name.getText().toString();
51                 String userpass = password.getText().toString();
52                 editor.putString("u_name",username);
53                 if("zhangsan".equals(username)&&"123".equals(userpass)){
54                     if(checkBox.isChecked()){
55                         editor.putString("u_pass",userpass);
56                         editor.commit();
57                     }else {
58                         editor.remove("u_pass");
59                         editor.commit();
60                     }
61                     Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
62                 }else {
63                     Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
64                 }
65                 editor.commit();
66                 break;
67             case  R.id.btn_reset:
68                 name.setText("");
69                 password.setText("");
70                 checkBox.setChecked(false);
71                 break;
72 
73         }
74     }
75 }
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9076760.html