使用SharedPreferences存储用户配置信息

 

 

  用SharedPreferences来保存用户的基本配置信息非常的方便,实现起来也很容易;以下是一个简单的例子:

 

效果截图:

      

 

主要代码:

 1 public class MainActivity
 2         extends AppCompatActivity
 3         implements View.OnClickListener {
 4     private EditText mEtName;
 5     private EditText mEtPassword;
 6     private CheckBox mCbMark;
 7     private Button   mBtnOk;
 8     private final String KEY_NAME     = "name";
 9     private final String KEY_PASSWORD = "password";
10     private final String KEY_MARK     = "mark";
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16 
17         initViews();
18     }
19 
20 
21     @Override
22     public void onClick(View v) {
23         String name = mEtName.getText().toString().trim();
24         String password = mEtPassword.getText().toString().trim();
25         if ("".equalsIgnoreCase(name) || "".equalsIgnoreCase(password)) {
26             Toast.makeText(this, "账号和密码不能为空!", Toast.LENGTH_LONG).show();
27         }
28         else {
29             SharedPreferences userInfo = getSharedPreferences("user_info", MODE_PRIVATE);
30             SharedPreferences.Editor editor = userInfo.edit();
31             editor.putString(KEY_NAME, name);
32             editor.putBoolean(KEY_MARK, mCbMark.isChecked());
33             if (mCbMark.isChecked()) {
34                 editor.putString(KEY_PASSWORD, password);
35             }
36             else {
37                 editor.putString(KEY_PASSWORD, "");
38             }
39             editor.apply();//---应用操作
40 
41             Toast.makeText(this, "用户信息保存成功!", Toast.LENGTH_LONG).show();
42         }
43     }
44 
45     private void initViews() {
46         SharedPreferences userInfo = getSharedPreferences("user_info", MODE_PRIVATE);
47 
48         mEtName     = (EditText)findViewById(R.id.etName);
49         mEtPassword = (EditText)findViewById(R.id.etPassword);
50         mCbMark     = (CheckBox)findViewById(R.id.cbMark);
51         mBtnOk      = (Button)findViewById(R.id.btnOk);
52 
53         mBtnOk.setOnClickListener(this);
54         mEtName.setText(userInfo.getString(KEY_NAME, ""));
55         if (userInfo.getBoolean(KEY_MARK, true)) {
56             mEtPassword.setText(userInfo.getString(KEY_PASSWORD, ""));
57             mCbMark.setChecked(true);
58         }
59         else {
60             mEtPassword.setText("");
61             mCbMark.setChecked(false);
62         }
63 
64     }
65 }

 

 

布局文件:

 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:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.haoye.preferencestest.MainActivity"
11     android:orientation="vertical"
12     android:gravity="center_horizontal"
13     android:weightSum="100">
14 
15     <EditText
16         android:id="@+id/etName"
17         android:layout_marginTop="20dp"
18         android:layout_width="300dp"
19         android:layout_height="50dp"
20         android:textSize="24sp"
21         android:hint="@string/name_hint"/>
22     <EditText
23         android:id="@+id/etPassword"
24         android:layout_marginTop="20dp"
25         android:layout_width="300dp"
26         android:layout_height="50dp"
27         android:inputType="textPassword"
28         android:textSize="24sp"
29         android:hint="@string/password_hint"/>
30     <CheckBox
31         android:id="@+id/cbMark"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:checked="true"
35         android:text="@string/cb_mark"/>
36     <Button
37         android:id="@+id/btnOk"
38         android:layout_marginTop="20dp"
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:text="@string/btn_ok"/>
42 
43 </LinearLayout>


resources 文件:

<resources>
    <string name="app_name">PreferencesTest</string>
    <string name="name_hint">请输入用户名</string>
    <string name="password_hint">请输入密码</string>
    <string name="btn_ok">确定</string>
    <string name="cb_mark">记住密码</string>
</resources>

 

posted @ 2016-03-18 16:18  (•̀ω•́)y  阅读(577)  评论(0编辑  收藏  举报