安卓SharedPreferences存储
activity_main.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=".MainActivity">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:maxLines="2"
android:textSize="20sp"
android:textStyle="italic"/>
<EditText
android:id="@+id/edt_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄"
android:maxLines="2"
android:textSize="20sp"
android:textStyle="italic"/>
<RadioGroup
android:id="@+id/rdg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbt_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="男"/>
<RadioButton
android:id="@+id/rbt_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="女"/>
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chk_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/chk_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/chk_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/chk_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写代码"
android:textSize="18sp"/>
</RadioGroup>
<Spinner
android:id="@+id/sp_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/classes"/>
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textSize="20sp"/>
《/LinearLayout>
strings.xml文件
My A<array name="classes">
<item>19软件工程1班</item>
<item>19软件工程2班</item>
<item>18软件工程1班</item>
<item>18软件工程2班</item>
</array>
MainActivity.java文件
package com.example.mya;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn_ok;
private TextView tv_show;
private EditText edt_name;
private EditText edt_age;
private RadioButton rbt_boy;
private Spinner sp_class;
private String class_name;
private CheckBox chk_1;
private CheckBox chk_2;
private CheckBox chk_3;
private CheckBox chk_4;
private RadioButton rbt_girl;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences personInfo = getSharedPreferences("personInfo",MODE_PRIVATE);
btn_ok = findViewById(R.id.btn_ok);
tv_show = findViewById(R.id.tv_show);
edt_name = findViewById(R.id.edt_name);
edt_age = findViewById(R.id.edt_age);
rbt_boy = findViewById(R.id.rbt_boy);
rbt_girl = findViewById(R.id.rbt_girl);
sp_class = findViewById(R.id.sp_class);
chk_1 = findViewById(R.id.chk_1);
chk_2 = findViewById(R.id.chk_2);
chk_3 = findViewById(R.id.chk_3);
chk_4 = findViewById(R.id.chk_4);
readFromSP();
sp_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// 输入日志
Log.e("MainActivity", "position = " + position);
// 获取strings.xml中定义的数组内容
String[] classArray = getResources().getStringArray(R.array.classes);
class_name = classArray[position];
SharedPreferences.Editor editor = personInfo.edit();
editor.putInt("position",position);
editor.apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 快捷键 new 空格 CTRL+SHIFT+空格
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 让窗口弹出一个提示 Toast 吐司
// 注释 CTRL + /
// Toast.makeText(MainActivity.this,
// “您点击了按钮”, Toast.LENGTH_SHORT).show();
// 使用资源文件
// Toast.makeText(MainActivity.this,
// R.string.app_name+class_name, Toast.LENGTH_SHORT).show();
// 获取输入框的内容
String name = edt_name.getText().toString();
int age = Integer.parseInt(edt_age.getText().toString());
boolean isBoy = rbt_boy.isChecked();
boolean isGirl = rbt_girl.isChecked();
boolean chk1 = chk_1.isChecked();
boolean chk2 = chk_2.isChecked();
boolean chk3 = chk_3.isChecked();
boolean chk4 = chk_4.isChecked();
// int position = sp_class.is();
// boolean spclass = class_name.split();
// 判断多选框有没有选中
StringBuffer buffer = new StringBuffer();
if(chk_1.isChecked()){
buffer.append(chk_1.getText().toString());
// .append(",");
}
if(chk_2.isChecked()){
buffer.append(chk_2.getText().toString());
}
if(chk_3.isChecked()){
buffer.append(chk_3.getText().toString());
}
if(chk_4.isChecked()){
buffer.append(chk_4.getText().toString());
}
// tv_show.setText(“你好帅哥,” +class_name + “, " + name + “,喜欢” + buffer.toString());
if(isBoy){
Toast.makeText(MainActivity.this,
“帅哥:”+name+” 年龄:"+age+" “+class_name+” 爱好:"+buffer.toString(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,
“美女:”+name+" 年龄:"+age+" “+class_name+” 爱好:"+buffer.toString(), Toast.LENGTH_SHORT).show();
}
writeToSP(name,age,isBoy,isGirl,chk1,chk2,chk3,chk4);
Toast.makeText(MainActivity.this,“保存成功”,Toast.LENGTH_SHORT).show();
}
});
}
private void writeToSP(String name,int age,boolean isBoy,boolean isGirl,boolean chk1,boolean chk2,
boolean chk3,boolean chk4){
SharedPreferences personInfo = getSharedPreferences("personInfo",MODE_PRIVATE);
SharedPreferences.Editor edit = personInfo.edit();
SharedPreferences.Editor editor = personInfo.edit();
editor.apply();
edit.putString("name",name);
edit.putInt("age",age);
edit.putBoolean("isBoy",isBoy);
edit.putBoolean("isGirl",isGirl);
edit.putBoolean("chk1",chk1);
edit.putBoolean("chk2",chk2);
edit.putBoolean("chk3",chk3);
edit.putBoolean("chk4",chk4);
// edit.putBoolean(“spclass”,spclass);
edit.commit();
}
private void readFromSP(){
SharedPreferences personInfo = getSharedPreferences("personInfo",MODE_PRIVATE);
String name = personInfo.getString("name","");
int age = personInfo.getInt("age",0);
boolean isBoy = personInfo.getBoolean("isBoy",true);
boolean isGirl = personInfo.getBoolean("isGirl",true);
// String buffer = personInfo.getString(“buffer”,"");
boolean chk1 = personInfo.getBoolean(“chk1”,false);
boolean chk2 = personInfo.getBoolean(“chk2”,false);
boolean chk3 = personInfo.getBoolean(“chk3”,false);
boolean chk4 = personInfo.getBoolean(“chk4”,false);
// String spclass = personInfo.getBoolean(spclass,false);
// boolean spclass = personInfo.getBoolean(“spclass”,true);
edt_name.setText(name);
edt_age.setText(String.valueOf(age));
rbt_boy.setChecked(isBoy);
rbt_girl.setChecked(isGirl);
position=personInfo.getInt("position",0);
chk_1.setChecked(chk1);
chk_2.setChecked(chk2);
chk_3.setChecked(chk3);
chk_4.setChecked(chk4);
sp_class.setSelection(position,true);
// sp_class.setChecked(spclass);
// rbtGirl.setChecked(!isBoy);
}
}