SharedPreferences是一个轻量级存储工具,采用的存储结构是Key-Value键值对方式。
共享参数的存储介质是符合xml规范的配置文件。保存路径是: /data/data/应用包名/shared_prefs/文件名.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"
tools:context=".ShareWriteActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="姓名:"
android:textColor="@color/black"
android:textSize="17sp"
android:gravity="center"></TextView>
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_edit_selector"
android:hint="请输入姓名"
android:inputType="text"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="17sp"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="年龄:"
android:textColor="@color/black"
android:textSize="17sp"
android:gravity="center"></TextView>
<EditText
android:id="@+id/et_age"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_edit_selector"
android:hint="请输入年龄"
android:inputType="number"
android:maxLength="3"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="17sp"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="身高:"
android:textColor="@color/black"
android:textSize="17sp"
android:gravity="center"></TextView>
<EditText
android:id="@+id/et_height"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_edit_selector"
android:hint="请输入身高"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="17sp"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="体重:"
android:textColor="@color/black"
android:textSize="17sp"
android:gravity="center"></TextView>
<EditText
android:id="@+id/et_weight"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_edit_selector"
android:hint="请输入体重"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:textSize="17sp"></EditText>
</LinearLayout>
<CheckBox
android:id="@+id/ck_married"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已婚"
></CheckBox>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存到共享参数"></Button>
</LinearLayout>
package com.qidian.chapter06;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class ShareWriteActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
private CheckBox ck_married;
private EditText et_name;
private EditText et_age;
private EditText et_height;
private EditText et_weight;
private SharedPreferences preference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_write);
ck_married = findViewById(R.id.ck_married);
ck_married.setOnCheckedChangeListener(this);
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);
findViewById(R.id.btn_save).setOnClickListener(this);
preference = getSharedPreferences("config", Context.MODE_PRIVATE);
reload();
}
private void reload() {
String name = preference.getString("name", "");
int age = preference.getInt("age", 0);
float height = preference.getFloat("height",0);
float weight = preference.getFloat("weight", 0);
boolean married = preference.getBoolean("married",false);
if (!name.isEmpty()) {
et_name.setText(name);
}
if (age > 0) {
et_age.setText(String.valueOf(age));
}
if (height > 0) {
et_height.setText(String.valueOf(height));
}
if (weight > 0) {
et_weight.setText(String.valueOf(weight));
}
ck_married.setChecked(married);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
@Override
public void onClick(View v) {
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 editor = preference.edit();
editor.putString("name",name);
editor.putInt("age",Integer.parseInt(age));
editor.putFloat("height",Float.parseFloat(height));
editor.putFloat("weight",Float.parseFloat(weight));
editor.putBoolean("married",ck_married.isChecked());
editor.commit();
}
}