什么是sharedPreferences存储
Share Preferences存储常用来存储一些**轻量级**的数据,以key-value键值对形式存储数据,,当用户卸载此应用程序时,数据会一并清除。
1.Sharedpreference是Android平台上一个用来存储少量1数据时简单、,便捷(如保存佳密码码状找态,设置开关状态等)。
2.以key-value键值对形式存储,可以存的数据类型为: String、 float、int、long. \boolean。
3.存储位置在/data/data/<包名>/shared-prefs
4保存的数据以XML形式存储
使用sharedPreferences写入数据步骤
1.获得sharedPreferences对象
2.获得editor对象
3.通过editor对象的putXXX函数,设置写入数据
4.最后一步要提交
获取sharedPreferences对象
在有 Context环境中使用 getshared Preferences方法获得
MODE_PRIVATE是默认操作模式,写入的内容会覆盖之前的内容。
MODE_APPEND会检查文件是否存在。
使用sharedPreferences案例
XML布局的代码:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="myapplication.com.example.service.MainActivity">
<EditText
android:id="@+id/main_zhanghao"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="输入账号" />
<EditText
android:id="@+id/main_password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/main_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登录" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/main_check"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="记住密码"
android:textSize="20sp" />
<CheckBox
android:id="@+id/main_check1"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:text="自动登录"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
接下来就是在activity里面绑定id和添加button点击事件,然后创建SharedPreferences对象,然后创建Editor对象,写入值,最后一步要提交。要记得从sp文件(text.soo)里取出“name”节点对应的值,然后把值赋给账号。这样的代码就会保存账号。如果我们想既保存密码又保存账号,我们需要对复选框做一判断。
public class MainActivity extends AppCompatActivity {
private EditText zhanghaoEdit;
private Button dengBtn;
private EditText passwordEdit;
private CheckBox CB;
private int CBFlag=0;
private String word="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
//从sp文件(text.soo)里取出“name”节点对应的值
SharedPreferences sharedPreferences = getSharedPreferences("text.soo", MODE_PRIVATE);
if (sharedPreferences != null) {
String name = sharedPreferences.getString("name", "");
word=sharedPreferences.getString("word","");
//赋值给zhanghaoEdit
zhanghaoEdit.setText(name);
CBFlag = sharedPreferences.getInt("cb_flag", 0);
}
if (CBFlag == 1) {
CB.setChecked(true);
passwordEdit.setText(word);
}
dengBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String zhanghao = zhanghaoEdit.getText().toString();
String word = passwordEdit.getText().toString();
//1.创建SharedPreferences对象
SharedPreferences sp = getSharedPreferences("text.soo", MODE_PRIVATE);
//2.创建Editor对象,写入值
SharedPreferences.Editor ed = sp.edit();
ed.putString("name", zhanghao);
if (CB.isChecked()) {
CBFlag = 1;
ed.putInt("cb_flag", CBFlag);
ed.putString("word", word);
} else {
CBFlag = 0;
ed.putInt("cb_flag", CBFlag);
}
//3.提交
ed.commit();
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
});
}
private void bindID() {
zhanghaoEdit = findViewById(R.id.main_zhanghao);
dengBtn = findViewById(R.id.main_btn);
passwordEdit = findViewById(R.id.main_password);
CB = findViewById(R.id.main_check);
}
}