SharedPreferences
SharedPreferences是什么
- SharedPreferences是一种轻量级的数据存储方式,通常用来存储一些简单的配置消息。比如:用户名密码,是否是第一次登陆,是否显示引导页。
如何存储数据
SharedPreferences sharedPreferences = getSharedPreferences("username", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username","张三");
editor.putString("psw","asd");
editor.commit();
如何读取数据
SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE);
String name=sharedPreferences.getString("username","");
String psw=sharedPreferences.getString("psw","");
neicun_name.setText(name);
neicun_password.setText(psw);
记住密码案例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/neicun_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的名字"
android:id="@+id/neicun_ed"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/neicun_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="30dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的密码"
android:id="@+id/neicun_ed1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/neicun_rb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:id="@+id/neicun_rb1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/neicun_btn"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="注册"
/>
</LinearLayout>
SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE)
final int checked=sharedPreferences.getInt("checked",0)
if (checked==1){
String name=sharedPreferences.getString("username","")
String psw=sharedPreferences.getString("psw","")
neicun_name.setText(name)
neicun_password.setText(psw)
rd.setChecked(true)
}else {
rd.setChecked(false)
}
bt.setOnClickListener(new View.OnClickListener() {
SharedPreferences sharedPreferences=getSharedPreferences("username",MODE_PRIVATE)
SharedPreferences.Editor editor=sharedPreferences.edit()
public void onClick(View view) {
if (rd.isChecked()){
String name=neicun_name.getText().toString()
String psw=neicun_password.getText().toString()
editor.putString("username",name)
editor.putString("psw",psw)
editor.putInt("checked",1)
}else {
editor.putString("username","")
editor.putString("psw","")
editor.putInt("checked",0)
}
editor.commit()
}
})
}