Android:通过SharedPreferences将数据保存到xml上

一、前言:什么情况下需要把数据保存到xml上呢?那我们日常使用的qq来说,当我们qq登录有好几个账号的时候点击下拉框会出现上次登录的数据。像这种情况就是使用了sharePrefereences。这种情况适合简单数据存储,复杂数据存储还是要用到数据库。

二、上代码

界面布局

<?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"
    android:padding="5dp"
    tools:context=".share.ShareWriteActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="姓名:"
            android:textColor="@color/black"
            android:textSize="17dp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:textColorHint="@color/gray"
            android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="@color/black"
            android:textSize="17dp"/>
        <EditText
            android:id="@+id/et_age"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入年龄"
            android:textColorHint="@color/gray"
            android:layout_weight="1" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="身高:"
            android:textColor="@color/black"
            android:textSize="17dp"/>
        <EditText
            android:id="@+id/et_height"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入身高"
            android:textColorHint="@color/gray"
            android:layout_weight="1" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="体重:"
            android:textColor="@color/black"
            android:textSize="17dp"/>
        <EditText
            android:id="@+id/et_weight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="请输入体重"
            android:textColorHint="@color/gray"
            android:layout_weight="1" />

    </LinearLayout>
    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="已婚"/>
    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存到共享参数"></Button>

</LinearLayout>

与之对应的Activity文件ShareWriteActivity

public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_write);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);
        findViewById(R.id.btn_save).setOnClickListener(this);

        preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        reload();
    }

    private void reload() {
        String name = preferences.getString("name", null);
        if (name != null){
            et_name.setText(name);
        }
        Integer age = preferences.getInt("age", 0);
        if (age != 0 ){
            et_age.setText(String.valueOf(age));
        }
        float height = preferences.getFloat("height", 0f);
        if (height != 0) {
            et_height.setText(String.valueOf(height));
        }
        float weight = preferences.getFloat("weight", 0f);
        if (weight != 0f){
            et_weight.setText(String.valueOf(weight));
        }
        boolean married = preferences.getBoolean("married", false);
        ck_married.setChecked(married);
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight =et_weight.getText().toString();
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("name",name);
        edit.putInt("age", Integer.parseInt(age));
        edit.putFloat("hight", Float.parseFloat(height));
        edit.putFloat("weight", Float.parseFloat(weight));
        edit.putBoolean("married",ck_married.isChecked());
        edit.commit();
        Toast.makeText(this, "已经保存", Toast.LENGTH_SHORT).show();
    }
}

三、总结:

共享参数使用的场景:

1.简单且孤立的数据。若是复杂且相互间有关的数据,则要保存在数据库中。

2.文本形式的数据。若是二进制数据,则要保存在文件中。

3.需要持久化的数据。在App退出后再次启动时,之前保存的数据仍然有效。

4.实际开发中,共享参数经常存储的数据化App的个性化配置信息、用户使用App的行为信息、临时需要保存的片段信息等。

存储数据

保存数据一般分为四个步骤:

  1. 使用Activity类的getSharedPreferences方法获得SharedPreferences对象;
  2. 使用SharedPreferences接口的edit获得SharedPreferences.Editor对象;
  3. 通过SharedPreferences.Editor接口的putXXX方法保存key-value对;
  4. 通过过SharedPreferences.Editor接口的commit方法保存key-value对。
读取数据

读取数据一般分为两个步骤:

  1. 使用Activity类的getSharedPreferences方法获得SharedPreferences对象;
  2. 通过SharedPreferences对象的getXXX方法获取数据;
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值