SharePreferences

本文介绍SharedPreferences的基本概念及使用方法,包括数据的存储与读取,并通过一个记住密码的案例演示了其在实际应用中的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SharePreferences

  • SharePreferences是什么
  • 如何存储数据
  • 如何读取数据
  • 记住密码案例

SharePreferences是什么

SharedPreferences常用来存储一些轻量级的数据,以key-vaiue(键值对)形式存储数据,当用户卸载此应用程序时,数据一并清除。

如何存储数据

  1. 获取SharePreferences对象;
  2. 获取Editor对象;
  3. 通过Editor对象的putXXX函数,设置写入数据;
  4. 通过Editor对象的commit提交写入。
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("name","张三");
        editor.commit();

如何读取数据

String name=sharedPreferences.getString("name","");
        nameTV.setText(name);

记住密码案例

<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"
    tools:context=".dengluActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FC4349">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/back_btn_white"
            android:layout_gravity="center"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="登录"
            android:gravity="center"
            android:textSize="25dp"
            android:textColor="#ffffff"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="9"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/yonghumingET"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/mimaET"/>

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

            <Button
                android:id="@+id/dengluBTO"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="登录"/>

            <CheckBox
                android:id="@+id/mimaBOX"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住密码"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
public class dengluActivity extends AppCompatActivity implements View.OnClickListener{
private EditText yonghumingET,mimaET;
private Button dengluBTO;
private CheckBox mimaBOX;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denglu);
        bind();
        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","");
                    yonghumingET.setText(name);
                    mimaET.setText(psw);
                    mimaBOX.setChecked(true);
                }else {
                    mimaBOX.setChecked(false);
                }
    }

    private void bind() {
        yonghumingET=findViewById(R.id.yonghumingET);
        mimaET=findViewById(R.id.mimaET);
        dengluBTO=findViewById(R.id.dengluBTO);
        mimaBOX=findViewById(R.id.mimaBOX);
        dengluBTO.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dengluBTO:
                SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                if (mimaBOX.isChecked()){
                    String username=yonghumingET.getText().toString();
                    String psw=mimaET.getText().toString();
                    editor.putString("username",username);
                    editor.putString("psw",psw);
                    editor.putInt("checked",1);
                    }else {
                    editor.putString("username","");
                    editor.putString("psw","");
                    editor.putInt("checked",0);
                }
                editor.commit();
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值