SharedPreferences介绍
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出.
SharedPreferences数据的四种操作模式
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入
SharedPreferences存储
SharedPreferences以xml的形式保存在 data/data/<包名>/shared_prefs 这个目录下
使用SharedPreferences写入数据步骤
- 获得使用SharedPreferences对象
- 获得Editor对象
- 通过Editor对象的getXXX函数,设置写入数据
- 通过Editor对象的commit提交写入
使用SharedPreferences读取数据步骤
- 获得使用SharedPreferences对象
- 通过SharedPreferences对象的getXXX方法获得对应类型的数据
使用SharedPreferences的实例
获取SharedPreferences中的数据显示出来用户名与密码
xml代码
<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"
android:orientation="vertical"
tools:context="com.lenovo.liu.pritace.SharedPreActivity">
<EditText
android:id="@+id/user_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入账号"/>
<EditText
android:id="@+id/posw_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
//运用CheckBox方法显示复选框
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<Button
android:id="@+id/deng_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
</LinearLayout>
Activity代码
public class SharedPreActivity extends AppCompatActivity {
//定义xml中写的控件
private EditText userEdit;
private EditText poswEdit;
private CheckBox checkBox;
private Button dengBtn;
//定义一个boolean类型,用于判断复选框是否选中
private boolean poswords=false;
//全局定义密码框
private String posw="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_pre);
bindID();//绑定id
//获取SharedPreferences对象
SharedPreferences preferences=getSharedPreferences("con",MODE_PRIVATE);
if (preferences!=null){//判断SharedPreferences对象是否为空
//定义用户名与密码
String name=preferences.getString("name","");
posw=preferences.getString("posw","");
//在EditText写入多获取的用户名
userEdit.setText(name);
poswords=preferences.getBoolean("poswords",false);
}
//判断复选框在关掉APP是否被选中一直选中
if (poswords=true){
checkBox.setChecked(true);
//输出密码
poswEdit.setText(posw);
}
//设置监听事件
dengBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//转换成String格式
String username=userEdit.getText().toString();
String posword=poswEdit.getText().toString();
SharedPreferences sp=getSharedPreferences("con",MODE_PRIVATE);
//获得editor对象
SharedPreferences.Editor editor=sp.edit();
editor.putString("name",username);
//判断复选框是否被选中
if (checkBox.isChecked()){
poswords=true;
//获取editor中密码对象
editor.putString("posw",posword);
editor.putBoolean("poswords",poswords);
}else {
poswords=false;
editor.putBoolean("poswords",poswords);
}
editor.commit();
}
});
}
private void bindID() {
userEdit=findViewById(R.id.user_edit);
poswEdit=findViewById(R.id.posw_edit);
checkBox=findViewById(R.id.check_box);
dengBtn=findViewById(R.id.deng_btn);
}
}