Android数据存储(一):SharedPreferences

SharedPreferences提供存储一些简单的配置信息机制,可以存储一些用户登录名及密

码,以键值对的方式存储,下面介绍存储用户和密码,退出程序或返回时再次打开应用程

序 ,仍能显示原来用户输入的信息。SharedPreferences存储了用户输入的信息,并显示在界面上。

 

1.布局文件:输入用户名及密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"
      xmlns:tools = "http://schemas.andrengroid.com/tools"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:orientation = "vertical"
      >
                                        
      < TextView
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "用户名"
          tools:context = ".MainActivity"   />
                                        
      < EditText
          android:id = "@+id/name"
          android:layout_width = "fill_parent"
          android:layout_height = "wrap_content"
          android:inputType = "text"
          android:ems = "10"   >
      </ EditText >
      < TextView
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "密码"
          />
      < EditText
          android:id = "@+id/password"
          android:layout_width = "fill_parent"
          android:layout_height = "wrap_content"
          android:ems = "10"
          android:inputType = "textPassword"   />
                                        
  </ LinearLayout >

2.编写activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public   class   MainActivity  extends   Activity {
     private   EditText name;
     private   EditText pass;
     // String SAVEINFOS;
     @Override
     public   void   onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         name = (EditText)  this .findViewById(R.id.name);

        pass = (EditText) this.findViewById(R.id.password);

         SharedPreferences sharedPreferences = getSharedPreferences("logininfo",
                 MODE_PRIVATE);
         // 取出文件内存储的值
         String name1 = sharedPreferences.getString( "name" "" );
         String password = sharedPreferences.getString( "pass" "" );
         if   (name1 !=  null   && ! "" .equals(name1)) {
             name.setText(name1);
         }
         if   (password !=  null   && ! "" .equals(password)) {
             pass.setText(password);
         }
     }
     @Override
     public   boolean   onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return   true ;
     }
     @Override
     protected   void   onStop() {
         super .onStop();
         // 应用程序退出时调用stop方法,此方法内保存输入的参数
         String nametext = name.getText().toString();
         String passtext = pass.getText().toString();
         // 将参数存储到logininfo文件内
        SharedPreferences share = getSharedPreferences( "logininfo",
                 Context.MODE_PRIVATE);
         // 得到编辑器,存储参数值
         Editor editor = share.edit();
         editor.putString( "name" , nametext);
         editor.putString( "pass" , passtext);
         editor.commit();
     }
  }

3.演示效果



退出程序再打开


 

生成的文件logininfo在DDMSdata/data/项目名/shared_pres下,导出是个xml格式的文件




logininfo。xml文件内容

1
2
3
4
<? xml   version = '1.0'   encoding = 'utf-8'   standalone = 'yes'   ?>
  < map > < string   name = "name" >夏普</ string >
       < string   name = "pass" >123456</ string >
  </ map >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值