第 6 章 数据存储全方案,详解 持久化技术
文件存储的方式并不适合用于保存一些较为复杂的文本数据
SharedPreferences 使用键值对的方式来存储数据的。支持多种不同数据的存储。
三种获取SharedPrefereces对象方法
1.Context.getSharedPreferences("wenjingming",MODE_PRIVATE);
①个是文件名,②个是操作类型。MODE_PRIVATE 是默认的操纵模式 MODE_MULTI_PROCESS 是多个线程同时操作一个SharedPreferences
2.Acitivity 类中getPrefereces() 方法,以类名作为文件名。参数自己指定。
3.PreferenceManager类中getDefaultSharedPreferences() 方法接受一个Context自动把当前的包名为前缀命名SharedPreferences 文件。
调用 SharedPreferences.edit() 方法来获取一个SharedPreferences.Editor对象。
用commit() 提交数据。
数据的保存
protected void save() {
// TODO Auto-generated method stub
SharedPreferences sp = getSharedPreferences("mySharedP", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("age", 28);
editor.putString("name", "XUDA");
editor.putBoolean("MARRIED", false);
editor.commit();
}
// TODO Auto-generated method stub
SharedPreferences sp = getSharedPreferences("mySharedP", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("age", 28);
editor.putString("name", "XUDA");
editor.putBoolean("MARRIED", false);
editor.commit();
}
数据的提取
SharedPreferences sp = getSharedPreferences("mySharedP", 0);
sp.getInt("age", 0);
Log.i("MainActivity", "name=" + sp.getString("name", "") + "age=" + sp.getInt("age", 0) + "");
sp.getInt("age", 0);
Log.i("MainActivity", "name=" + sp.getString("name", "") + "age=" + sp.getInt("age", 0) + "");
实现记住密码功能
public class MainActivity extends Activity {
private EditText et_username, et_password;
private SharedPreferences sp;
private SharedPreferences.Editor spe;
private Button btn_login;
private CheckBox cb_sava;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
btn_login = (Button) findViewById(R.id.btn_login);
cb_sava = (CheckBox) findViewById(R.id.cb_sava);
sp = PreferenceManager.getDefaultSharedPreferences(this);
spe = sp.edit();
if (sp.getBoolean("save", false)) {
et_username.setText(sp.getString("username", ""));
et_password.setText(sp.getString("password", ""));
}
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cb_sava.isChecked()) {
spe.putString("username", et_username.getText().toString().trim());
spe.putString("password", et_password.getText().toString().trim());
spe.putBoolean("save", true);
} else {
spe.clear();
}
spe.commit();
}
});
}
}
private EditText et_username, et_password;
private SharedPreferences sp;
private SharedPreferences.Editor spe;
private Button btn_login;
private CheckBox cb_sava;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
btn_login = (Button) findViewById(R.id.btn_login);
cb_sava = (CheckBox) findViewById(R.id.cb_sava);
sp = PreferenceManager.getDefaultSharedPreferences(this);
spe = sp.edit();
if (sp.getBoolean("save", false)) {
et_username.setText(sp.getString("username", ""));
et_password.setText(sp.getString("password", ""));
}
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cb_sava.isChecked()) {
spe.putString("username", et_username.getText().toString().trim());
spe.putString("password", et_password.getText().toString().trim());
spe.putBoolean("save", true);
} else {
spe.clear();
}
spe.commit();
}
});
}
}
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Account:" />
<EditText
android:id="@+id/et_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="Input your account"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Account:" />
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Password"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/cb_sava"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Remember password" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Account:" />
<EditText
android:id="@+id/et_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="Input your account"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Account:" />
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Password"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/cb_sava"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Remember password" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>