版权声明:如转载请表明出处 https://blog.csdn.net/weixin_42247720/article/details/89516573
数据存储(一)SharedPreferences轻量级数据存储
代码示例
SharedPreferencesActivity的java代码
package com.example.administrator.exercise.datastorage;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.administrator.exercise.R;
import com.example.administrator.exercise.util.ToastUtil;
public class SharedPreferencesActivity extends AppCompatActivity {
private EditText editText;
private Button button_save,button_display;
private TextView textView;
//读取数据
private SharedPreferences sharedPreferences;
//写入数据
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
editText = (EditText) findViewById(R.id.sp_et_input);
textView = (TextView) findViewById(R.id.sp_tv_display);
//连接保存按钮
button_save = (Button) findViewById(R.id.sp_btn_save);
//连接显示按钮
button_display = (Button) findViewById(R.id.sp_btn_display);
//实例化sharedPreferences,参数:文件名称,模式(通常使用PRIVATE)
sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
//实例化editor
editor = sharedPreferences.edit();
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//putString写入数据:键值对,参数:key,value
editor.putString("data",editText.getText().toString());
editor.apply();
//这个是自定义的Toast工具,理解为一般的Toast提示即可。
ToastUtil.showMsg(SharedPreferencesActivity.this,"保存成功");
}
});
button_display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//getString读出数据,参数:key,default(缺省的值,设为空即可)
textView.setText(sharedPreferences.getString("data",""));
ToastUtil.showMsg(SharedPreferencesActivity.this,"读取成功");
}
});
}
}
对应的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_shared_preferences"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入数据"
android:id="@+id/sp_et_input"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="保存"
android:textSize="16sp"
android:id="@+id/sp_btn_save"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="显示"
android:textSize="16sp"
android:id="@+id/sp_btn_display"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/sp_tv_display"/>
</LinearLayout>
运行结果图
文件保存位置
data/data/applicationID(也就是工程包名)/shared_prefs
总结
//定义读写对象
SharedPreferences sharedPreferences = getSharedPreferences(“参数名”,MODE_PRIVATE)
SharedPreferences.Editor editor = sharedPreferences.edit()
//写入数据
editor.putString(“参数名”,editText.getText().toString())
editor.apply();
//读出数据
sharedPreferences.getString(“参数名”,"")