SharedPreferences存储

1通过getSharedPreferences获得 SharedPreferences对象

2通过edit()方法获取Editor对象,

3通过Edtitor对象的putString()方法进行存储

4通过Editor对象的commit()方法进行提交

 

从存储中取出方法为String name=sharePreferences.getString()取出

 

Main

 1 package demo01.example.administrator.demo01;
 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.CheckBox;
 8 import android.widget.EditText;
 9 import android.widget.Toast;
10 
11 import static demo01.example.administrator.demo01.R.id.user_et;
12 
13 public class MainActivity extends AppCompatActivity {
14 EditText userName,passWord;
15     SharedPreferences sharedPreferences;
16     SharedPreferences.Editor editor;
17     CheckBox checkBox;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22         userName= (EditText) findViewById(R.id.user_et);
23         passWord= (EditText) findViewById(R.id.pass_et);
24         checkBox= (CheckBox) findViewById(R.id.checkBox);
25         sharedPreferences=getSharedPreferences("MyInfor",MODE_PRIVATE);
26         editor=sharedPreferences.edit();
27        String name=sharedPreferences.getString("useName","");
28         if (name==null){
29             checkBox.setChecked(false);
30 
31         }else {
32 //            checkBox.setChecked(true);
33             userName.setText(name);
34         }
35     }
36     public void doClick(View view){
37         switch (view.getId()){
38 
39             case R.id.login:
40                 String name=userName.getText().toString().trim();
41                 String password=passWord.getText().toString().trim();
42                 if(name.equals("admin")&&password.equals("123456")){

44                         editor.putString("useName",name);
45 
46                     editor.commit();
47                                   Toast.makeText(MainActivity.this,"成功登录",Toast.LENGTH_SHORT).show();
48                 }else {
49                     editor.remove("useName");
50                     editor.commit();
51                 }
52                 break;

58 
59         }
60     }

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6 <LinearLayout
 7     android:layout_width="match_parent"
 8     android:layout_height="wrap_content">
 9     <TextView
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:id="@+id/user"
13         android:text="用户名"
14         android:textSize="40sp"/>
15     <EditText
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:id="@+id/user_et"
19         android:layout_weight="1"
20         />
21 </LinearLayout>
22     <LinearLayout
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content">
25         <TextView
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:id="@+id/password"
29             android:text="密码"
30             android:textSize="40sp"/>
31         <EditText
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:id="@+id/pass_et"
35             android:layout_weight="1"/>
36     </LinearLayout>
37     <CheckBox
38         android:layout_width="wrap_content"
39         android:layout_height="wrap_content"
40         android:text="保存用户名"/>
41     <LinearLayout
42         android:layout_width="match_parent"
43         android:layout_height="wrap_content">
44         <Button
45             android:layout_width="wrap_content"
46             android:layout_height="wrap_content"
47             android:text="登录"
48             android:onClick="doClick"
49             android:id="@+id/login"/>
50         <Button
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:text="取消"
54             android:id="@+id/cancel"
55             />
56     </LinearLayout>
57 </LinearLayout>

 

转载于:https://www.cnblogs.com/633sylss/p/5497397.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值