SharePreferences

  • SharePreferences是什么
  • 如何存储数据
  • 如何读取数据
  • 案例–登录页面记住密码

    SharePreferences是什么

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

    存储数据

    得到SharedPreference对象之后,就可以开始向SharedPreference文件中存储数据了,主要有三步:

  • 使用SharedPreference对象的edit()方法来获取一个SharedPreference.Editor对象

  • 向SharedPreference.Editor对象中添加数据,比如添加一个布尔型数据就是用putBoolean方法,添加一个字符串就是用putString()方法,以此类推
  • 调用commit()方法将添加的数据提交,从而完成数据存储操作
private SharePreferences sp;

//创建一个SharePreferences接口的实例对象,将生成一个XML名称为demo_01,模式为MODE_PRIVATE

sp=this.getSharedPreferences("demo_01",MODE_PRIVATE);

//通过edit()方法创建一个SharePreferences.Editor类的实例对象
SharePreferences.Editor editor =sp.edit();
//通过putString()方法,将数据存入文件中
editor.putString("name","sssss");

//用commit()方法予以正式提交

editor.commit();

读取数据

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
String name = sp.getString("name", null);
int age = sp.getInt("age", 0);

案例

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private EditText name_ed, psw_ed;
    private Button login_btn, reg_btn;
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindID();

        SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        int checked = sharedPreferences.getInt("checked", 0);

        if (checked == 1) {
            String name = sharedPreferences.getString("username", "");
            String psw = sharedPreferences.getString("psw", "");
            name_ed.setText(name);
            psw_ed.setText(psw);
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
    }

    private void bindID() {
        name_ed = findViewById(R.id.ed1);
        psw_ed = findViewById(R.id.ed2);
        login_btn = findViewById(R.id.btn1);
        reg_btn = findViewById(R.id.btn2);
        checkBox = findViewById(R.id.cb);
        login_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                if (checkBox.isChecked()) {
                    String username = name_ed.getText().toString();
                    String password = psw_ed.getText().toString();
                    editor.putString("username", username);
                    editor.putString("psw", password);
                    editor.putInt("checked", 1);
                } else {
                    editor.putString("username", "");
                    editor.putString("psw", "");
                    editor.putInt("checked", 0);
                }
                editor.commit();

                break;

        }
    }
}
<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=".Main2Activity">

    <EditText
        android:id="@+id/ed1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/ed2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

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

            <Button
                android:id="@+id/btn1"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="登录" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/btn1"
                android:text="注册" />
        </LinearLayout>

    </RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="记住密码"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值