用于保存用户的配置数据,使软件关闭再次打开时还存在上次的操作数据
界面文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/name"
android:textSize="20sp"
android:hint="@string/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/age"
android:textSize="20sp"
android:hint="@string/age"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:onClick="save"
android:text="@string/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<resources>
<string name="app_name">MySharedPreferences</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
<string name="button">保存参数</string>
<string name="success">保存成功</string>
</resources>
主类
package com.example.mysharedpreferences;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText nameText;
private EditText ageText;
private PreferencesService sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText=this.findViewById(R.id.name);
ageText=this.findViewById(R.id.age);
//自己新建个类
//此类操作SharedPreferences---保存数据在XML文件夹
sp=new PreferencesService(this);
//获取配置参数并设置到输入框
//读取配置文件回显输入框
Map<String,String> par=sp.getPreferences();
nameText.setText(par.get("name"));
ageText.setText(par.get("age"));
}
public void save(View view) {
//获取姓名年龄
String name=nameText.getText().toString();
String age=ageText.getText().toString();
//新建个类
//PreferencesService sp=new PreferencesService(this);
//转入值
sp.save(name,Integer.valueOf(age));
//提示
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
}
}
package com.example.mysharedpreferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.HashMap;
import java.util.Map;
public class PreferencesService {
//上下文对象
private Context context;
public PreferencesService(Context context){
this.context=context;
}
//保存
public void save(String name, Integer valueOf) {
//获取SharedPreferences对象
//文件名----文件的操作模式
//this.getPreferences(mode);也可以获取SharedPreferences对象,会取本类作为文件名称
SharedPreferences sp=context.getSharedPreferences("itcast",context.MODE_PRIVATE);
//Editor编辑器对象
Editor editor=sp.edit();
//1存储在xml的名称----2参数值
//此时的数据是在内存中的,不是在文件中的
editor.putString("name",name);
editor.putInt("age",valueOf);
//需要调用commit方法把数据提交回文件中
editor.commit();
}
//读取
public Map<String,String> getPreferences(){
Map<String,String> par=new HashMap<String, String>();
SharedPreferences sp=context.getSharedPreferences("itcast",context.MODE_PRIVATE);
//1取出数据的名称----2如果数据不存在就返回这个字符串 sp.getString("name","")
//添加进集合
par.put("name",sp.getString("name",""));
par.put("age", String.valueOf(sp.getInt("age",0)));
//返回配置参数
return par;
}
}