SharePreferences
SharePreferences是以键值对的方式来存储数据的,通过context的getSharePreferences()方法,第一个参数是文件名,第二个参数是存储模式。存储完之后是通过xml文件来管理数据的。
- 例子:xml布局,通过两个editText和两个按钮,将编辑框中的内容保存,并在下次打开的时候自动读写在editText内
<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=".MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/userName"
android:hint="请输入用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:hint="请输入密码"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save"
android:text="save"/>
</LinearLayout>
- shared工具类
package com.example.sharedpreferencesdemo;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
public class SharedHelper {
Context context;
public SharedHelper(Context context) {
this.context = context;
}
public void saveData(String userName,String passWord){
SharedPreferences shared = context.getSharedPreferences("hello", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("userName",userName);
editor.putString("passWord",passWord);
editor.apply(); // apply()方法容易忘记
}
public Map<String,String> readData(){
Map<String, String> map = new HashMap<>();
SharedPreferences shared = context.getSharedPreferences("hello", Context.MODE_PRIVATE);
map.put("userName",shared.getString("userName","")); // 通过getString或者getint等方法输入键参数取出相应的值
map.put("passWord",shared.getString("passWord",""));
return map;
}
}
- mainActivity代码
package com.example.sharedpreferencesdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText userNameEd;
private EditText passWordEd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNameEd = findViewById(R.id.userName);
passWordEd = findViewById(R.id.password);
Button button = findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedHelper sharedHelper = new SharedHelper(getApplicationContext());
sharedHelper.saveData(userNameEd.getText().toString(),passWordEd.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedHelper sharedHelper = new SharedHelper(getApplicationContext());
Map<String, String> map = new HashMap<>();
map = sharedHelper.readData();
userNameEd.setText(map.get("userName"));
passWordEd.setText(map.get("passWord"));
}
}