SharePreferences
- SharePreferences是什么
- 如何存储数据
- 如何读取数据
- 记住密码案例
SharePreferences是什么
SharedPreferences常用来存储一些轻量级的数据,以key-vaiue(键值对)形式存储数据,当用户卸载此应用程序时,数据一并清除。
如何存储数据
- 获取SharePreferences对象;
- 获取Editor对象;
- 通过Editor对象的putXXX函数,设置写入数据;
- 通过Editor对象的commit提交写入。
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name","张三");
editor.commit();
如何读取数据
String name=sharedPreferences.getString("name","");
nameTV.setText(name);
记住密码案例
<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=".dengluActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FC4349">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/back_btn_white"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="登录"
android:gravity="center"
android:textSize="25dp"
android:textColor="#ffffff"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/yonghumingET"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mimaET"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/dengluBTO"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="登录"/>
<CheckBox
android:id="@+id/mimaBOX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
public class dengluActivity extends AppCompatActivity implements View.OnClickListener{
private EditText yonghumingET,mimaET;
private Button dengluBTO;
private CheckBox mimaBOX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_denglu);
bind();
SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
int checked=sharedPreferences.getInt("checked",0);
if (checked==1){
String name=sharedPreferences.getString("username","");
String psw=sharedPreferences.getString("psw","");
yonghumingET.setText(name);
mimaET.setText(psw);
mimaBOX.setChecked(true);
}else {
mimaBOX.setChecked(false);
}
}
private void bind() {
yonghumingET=findViewById(R.id.yonghumingET);
mimaET=findViewById(R.id.mimaET);
dengluBTO=findViewById(R.id.dengluBTO);
mimaBOX=findViewById(R.id.mimaBOX);
dengluBTO.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dengluBTO:
SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if (mimaBOX.isChecked()){
String username=yonghumingET.getText().toString();
String psw=mimaET.getText().toString();
editor.putString("username",username);
editor.putString("psw",psw);
editor.putInt("checked",1);
}else {
editor.putString("username","");
editor.putString("psw","");
editor.putInt("checked",0);
}
editor.commit();
break;
}
}
}