SharedPreferences是Android提供的一个轻量级存储类,经常用于保存软件设置参数。存放的格式为xml,文件存放在
/data/data/package name/shared_prefs下。
1.主布局文件 activity_main.xml
2.edittext_selector.xml文件
3.资源文件strings.xml
SharedPreferencesTest
用户名:
密码:
保存设置:
显示:
4.MainActivity.xml文件
public class MainActivity extends Activity {
private EditText et_name;
private EditText et_password;
private TextView resultText;
private CheckBox checkBox;
private boolean flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) this.findViewById(R.id.name);
et_password = (EditText) this.findViewById(R.id.password);
resultText = (TextView) this.findViewById(R.id.showText);
checkBox = (CheckBox) findViewById(R.id.check);
Button button = (Button) this.findViewById(R.id.button);
Button showButton = (Button) this.findViewById(R.id.showButton);
button.setOnClickListener(listener);
showButton.setOnClickListener(listener);
// 打开软件的时候回显
SharedPreferences sharedPreferences = getSharedPreferences("ljq123",
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
String nameValue = sharedPreferences.getString("name", "");
int ageValue = sharedPreferences.getInt("age", 1);
flag = sharedPreferences.getBoolean("checked", false);
System.out.println("flag:" + flag);
if (flag) {
checkBox.setChecked(true);
et_name.setText(nameValue);
et_password.setText(String.valueOf(ageValue));
}
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button button = (Button) v;
// dong文件存放在/data/data//shared_prefs目录下
SharedPreferences sharedPreferences = getSharedPreferences("dong",
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
switch (button.getId()) {
case R.id.button:
String name = et_name.getText().toString();
String password = et_password.getText().toString();
if (checkBox.isChecked()) {
flag = true;
} else {
flag = false;
}
if (flag) {
Editor editor = sharedPreferences.edit(); // 获取编辑器
editor.putString("name", name);
editor.putString("password", password);
editor.putBoolean("checked", flag);
editor.commit();// 提交修改
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG)
.show();
}
break;
case R.id.showButton:
String nameValue = sharedPreferences.getString("name", "");
String passwordValue = sharedPreferences.getString("password",
"");
resultText.setText("姓名:" + nameValue + ",密码:" + passwordValue);
break;
}
}
};
}
运行截图:
访问其它应用中的SharedPreferences文件
新建一个应用AccessSharePreferenceTest,建立一个单元测试testAccessPreference:
在application节点中增加
在application节点外增加
测试类
public class AccessSharePreferenceTest extends AndroidTestCase {
private static final String TAG = "AccessSharePreferenceTest";
public void testAccessPreference() throws Exception {
Context context = this.getContext().createPackageContext(
"com.example.sharedpreferencestest",
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = context.getSharedPreferences("dong",
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
String name = sp.getString("name", "");
String password = sp.getString("password", "");
Log.d(TAG, "name:" + name + " password:" + password);
}
}
运行截图: