SharedPreferences以一个Key-Value的方式去保存Android中的一些简单数据,如配置信息等。
本次讲解了SharedPreferences的一些基本使用方法。
第一次进入时,未保存数据,填写信息后再次进入则读取上次填写信息:
主程序:
public class Main extends Activity implements OnCheckedChangeListener
{
//该SharedPreferences的名字 同时也对应保存的文件名
private final String PREFERENCE_NAME = "survey";
private EditText etName;
private EditText etHabit;
private CheckBox cbEmployee;
private RadioGroup rgCompanyType;
private RadioButton rbCompany1;
private RadioButton rbCompany2;
private RadioButton rbCompany3;
@Override
protected void onStop()
{
//第一步
SharedPreferences mySharedPreferences = getSharedPreferences(
PREFERENCE_NAME, Activity.MODE_PRIVATE);
//第二步
SharedPreferences.Editor editor = mySharedPreferences.edit();
//第三步
editor.putString("name", etName.getText().toString());
editor.putString("habit", etHabit.getText().toString());
editor.putBoolean("employee", cbEmployee.isChecked());
editor.putInt("companyTypeId", rgCompanyType.getCheckedRadioButtonId());
//第四步
editor.commit();
super.onStop();
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
rbCompany1.setEnabled(isChecked);
rbCompany2.setEnabled(isChecked);
rbCompany3.setEnabled(isChecked);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etName = (EditText) findViewById(R.id.etName);
etHabit = (EditText) findViewById(R.id.etHabit);
cbEmployee = (CheckBox) findViewById(R.id.cbEmployee);
rgCompanyType = (RadioGroup) findViewById(R.id.rgCompanyType);
rbCompany1 = (RadioButton) findViewById(R.id.rbCompany1);
rbCompany2 = (RadioButton) findViewById(R.id.rbCompany2);
rbCompany3 = (RadioButton) findViewById(R.id.rbCompany3);
cbEmployee.setOnCheckedChangeListener(this);
SharedPreferences sharedPreferences = getSharedPreferences(
PREFERENCE_NAME, Activity.MODE_PRIVATE);
//读取数据
etName.setText(sharedPreferences.getString("name", ""));
etHabit.setText(sharedPreferences.getString("habit", ""));
cbEmployee.setChecked(sharedPreferences.getBoolean("employee", false));
rgCompanyType.check(sharedPreferences.getInt("companyTypeId", -1));
onCheckedChanged(cbEmployee, cbEmployee.isChecked());
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="姓名"
android:textSize="20dp" />
<EditText android:id="@+id/etName" android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="爱好"
android:textSize="20dp" />
<EditText android:id="@+id/etHabit" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/cbEmployee " android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="工作" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="单位"
android:textSize="20dp" />
<RadioGroup android:id="@+id/rgCompanyType" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/rbCompany1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="单位1" />
<RadioButton android:id="@+id/rbCompany2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="单位2" />
<RadioButton android:id="@+id/rbCompany3"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="单位3" />
</RadioGroup>
</LinearLayout>
SharedPreferences保存在手机的私有目录下/data/data/<packagename>/Shared_Prefs目录下,文件名和程序中使用的文件名相同。