0319-SharePreference知识点

SharePreference是什么?

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。

SharedPreferences数据的四种操作模式:

Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容

Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.

MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.

MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入

特别注意:出于安全性的考虑,MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE 在Android 4.2版本中已经被弃用

SharedPreferences使用方法:

1、得到SharedPreferences对象

2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。

3、向SharedPreferences.Editor对象中添加数据。

4、调用commit方法将添加的数据提交。

使用SharedPreferences实例(添加复选框):

创建一个Activity,布置xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.test.project.shujucunchuapplication.Sp1Activity">


    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:hint="输入账号"
        />
    <EditText
        android:id="@+id/pass_word"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:hint="输入密码"
        android:inputType="textPassword"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="5dp"
        />
    <TextView
        android:id="@+id/text_jzmm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:layout_toRightOf="@+id/checkbox"
        android:textSize="20sp"
        android:paddingTop="5dp"
        />

        <Button
            android:id="@+id/button_login"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:text="登录"
            android:layout_toRightOf="@+id/text_jzmm"
            />
    </RelativeLayout>
</LinearLayout>

布置java代码:

package com.test.project.shujucunchuapplication;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Sp1Activity extends AppCompatActivity {

    private EditText userName;
    private EditText passWord;
    private CheckBox checkBox;
    private Button button;

    //定义一个标志位,判断checkBox到底有没有被选中
    private int rememberFlag;

    private String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sp1);
        bindId();
        //从Sp文件(test.xml)里取出"name"节点对应的值;
        SharedPreferences sharedPreferences=getSharedPreferences("test.xml",MODE_PRIVATE);
        if (sharedPreferences!=null){
            //如果有test.xml文件,就取出里面的值
            String name=sharedPreferences.getString("name","");
            password=sharedPreferences.getString("password","");

            //把取出的值赋给userName
            userName.setText(name);
            rememberFlag=sharedPreferences.getInt("remember_flag",0);

        }

        if (rememberFlag==1){
            checkBox.setChecked(true);
            passWord.setText(password);
        }


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String username=userName.getText().toString();
                String password=passWord.getText().toString();
                //1.创建SharePreferences对象
                SharedPreferences sp = getSharedPreferences("test.xml",Context.MODE_PRIVATE);
                //2.创建Editor对象
                SharedPreferences.Editor ed = sp.edit();
                ed.putString("name", username);

                Toast.makeText(Sp1Activity.this,"登录成功",Toast.LENGTH_SHORT).show();


                //判断复选框有没有被选中
                if (checkBox.isChecked()){
                    rememberFlag=1;
                    ed.putInt("remember_flag",rememberFlag);
                    ed.putString("password",password);
                }else { 
                    rememberFlag=0;
                    ed.putInt("remember_flag",rememberFlag);
                }
                //提交
                ed.commit();

            }
        });



    }

    private void bindId() {
        userName = findViewById(R.id.user_name);
        passWord = findViewById(R.id.pass_word);
        checkBox = findViewById(R.id.checkbox);
        button = findViewById(R.id.button_login);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值