-
SharedPreferences是Android平台提供的一个轻量级的存储类,存储格式是key-value(键值对),使用xml文件保存数据,文件存放在/data/data/package-name/shared_prefs目录下。
-
基本使用,可用于软件的基本设置,或者是保存用户的基本信息。
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("name", "wxq").commit();
sharedPreferences.edit().putString("age", "11").commit();
可在adb shell下/data/data/package-name/shared_prefs/目录下找到保存的数据文件
查看config.xml文件,可以看到保存的数据
3. 下面使用SharedPreferences保存登录用户名和密码(示例,密码不会这样保存)
输入用户名和密码后,点击保存,用户名和密码保存到/data/data/package-name/shared_prefs/目录下,当重新启动软件,点击读取,读取上次保存的数据。
直接贴代码了。。。
布局文件
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/name_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/pass_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入密码"
android:inputType="numberPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<Button
android:id="@+id/save_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存" />
<Button
android:id="@+id/read_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="读取" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText nameEt, passEt;
private Button saveBtn, readBtn;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
initVies();
initListener();
}
private void initVies() {
nameEt = (EditText) findViewById(R.id.name_edit);
passEt = (EditText) findViewById(R.id.pass_edit);
saveBtn = (Button) findViewById(R.id.save_btn);
readBtn = (Button) findViewById(R.id.read_btn);
}
private void initListener() {
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.isEmpty(nameEt.getText().toString())) {
sharedPreferences.edit()
.putString("name", nameEt.getText().toString())
.commit();
}
if (!TextUtils.isEmpty(passEt.getText().toString())) {
sharedPreferences.edit()
.putString("pass", passEt.getText().toString())
.commit();
}
}
});
readBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameEt.setText(sharedPreferences.getString("name", ""));
passEt.setText(sharedPreferences.getString("pass", ""));
}
});
}
}
点击保存按钮,获取用户名和密码保存到config.xml文件中。
点击读取按钮,获取config.xml文件中的用户名和密码显示到控件上。