SharedPreferences

SharedPreferences

SharedPreferences是什么

  • SharedPreferences是一种轻量级的数据存储方式,通常用来存储一些简单的配置消息。比如:用户名密码,是否是第一次登陆,是否显示引导页。

如何存储数据

           //实例化SharedPreferences对象(第一步)
            SharedPreferences sharedPreferences = getSharedPreferences("username", MODE_PRIVATE);
            //实例化SharedPreferences.Editor对象(第二步)
            SharedPreferences.Editor editor = sharedPreferences.edit();
            //用putString的方法保存数据
            editor.putString("username","张三");
            editor.putString("psw","asd");
            //提交当前数据 
            editor.commit();

如何读取数据

//在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
        SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
        // 使用getString方法获得value,注意第2个参数是value的默认值
        String name=sharedPreferences.getString("username","");
        String psw=sharedPreferences.getString("psw","");
        //用settext方法替换成文件中保存的内容
        neicun_name.setText(name);
        neicun_password.setText(psw);

记住密码案例

  • 布局代码如下

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/neicun_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="30dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入你的名字"
            android:id="@+id/neicun_ed"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/neicun_tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="30dp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入你的密码"
            android:id="@+id/neicun_ed1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:id="@+id/neicun_rb"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            android:id="@+id/neicun_rb1"
            />
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
              <Button
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:text="登录"
                  android:id="@+id/neicun_btn"
                  />
               <Button
                   android:layout_width="0dp"
                   android:layout_weight="1"
                   android:layout_height="wrap_content"
                   android:text="注册"
                   />
        </LinearLayout>
  • Activity代码如下
   SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
         final int checked=sharedPreferences.getInt("checked",0);
         if (checked==1){
             String name=sharedPreferences.getString("username","");
             String psw=sharedPreferences.getString("psw","");
             neicun_name.setText(name);
             neicun_password.setText(psw);
             rd.setChecked(true);
         }else {
              rd.setChecked(false);
         }

         bt.setOnClickListener(new View.OnClickListener() {

             SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
              SharedPreferences.Editor editor=sharedPreferences.edit();
             public void onClick(View view) {
                    if (rd.isChecked()){
                        String name=neicun_name.getText().toString();
                        String psw=neicun_password.getText().toString();
                        editor.putString("username",name);
                        editor.putString("psw",psw);
                       editor.putInt("checked",1);

                    }else {

                        editor.putString("username","");
                        editor.putString("psw","");
                        editor.putInt("checked",0);
                    }
                 editor.commit();
             }
         });
           }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值