android:用Sharedpreferences保存和读取数据
一,代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.PreferenceActivity">
<Button
android:id="@+id/buttonsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="155dp"
android:layout_marginBottom="653dp"
android:text="保存"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="174dp"
android:layout_marginTop="65dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="539dp"
android:text="读取"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonsave" />
</androidx.constraintlayout.widget.ConstraintLayout>
java:
package com.example.okdemo1.activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.imagelib.ImageListActivity;
import com.example.okdemo1.MainActivity;
import com.example.okdemo1.R;
public class PreferenceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_preference);
//给按钮增加点击事件
Button myButton = findViewById(R.id.buttonsave);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理点击事件
//获取一个文件名为userInfo、权限为private的xml文件的SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
//得到SharedPreferences.Editor对象,并保存数据到该对象中
SharedPreferences.Editor editor = sharedPreferences.edit();
//保存key-value对到文件中
editor.putInt("id", 12345);
editor.putString("name", "总统秘书王富贵");
editor.commit();
}
});
//给按钮增加点击事件
Button readButton = findViewById(R.id.buttonread);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理点击事件
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
//读取数据
int id = sharedPreferences.getInt("id", 0);
String name = sharedPreferences.getString("name", "");
//打印已读取数据
Log.d("id",String.valueOf(id));
Log.d("name",name);
}
});
}
}
二,测试效果

三,查看文件在手机文件系统中保存的位置:
1,打开设备浏览器

2,文件路径:/data/data/包名/shared_prefs/


浙公网安备 33010602011771号