- public class SPActivity extends Activity {
- /**使用SharedPreferences 来储存与读取数据**/
- SharedPreferences mShared = null;
- /**程序中可以同时存在多个SharedPreferences数据, 根据SharedPreferences的名称就可以拿到对象**/
- public final static String SHARED_MAIN = "main";
- /**SharedPreferences中储存数据的Key名称**/
- public final static String KEY_NAME = "name";
- public final static String KEY_NUMBER = "number";
- /**SharedPreferences中储存数据的路径**/
- public final static String DATA_URL = "/data/data/";
- public final static String SHARED_MAIN_XML = "main.xml";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.sharedpreferences);
- /**拿到名称是SHARED_MAIN 的SharedPreferences对象**/
- mShared = getSharedPreferences(SHARED_MAIN, Context.MODE_PRIVATE);
- /**拿到SharedPreferences中保存的数值 第二个参数为如果SharedPreferences中没有保存就赋一个默认值**/
- String name = mShared.getString(KEY_NAME, "数据库中没有储存姓名");
- String number = mShared.getString(KEY_NUMBER, "数据库中没有储存号码");
- final EditText editName = (EditText)findViewById(R.id.sp_et0);
- final EditText editNumber = (EditText)findViewById(R.id.sp_et1);
- editName.setHint("上次输入的姓名为【 " +name+"】");
- editNumber.setHint("上次输入的号码为【 " +number+"】");
- Button button0 = (Button)findViewById(R.id.sp_button0);
- /**监听按钮点击后保存用户输入信息到SharedPreferences中**/
- button0.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- /**拿到用户输入的信息**/
- String name = editName.getText().toString();
- String number = editNumber.getText().toString();
- /**开始保存入SharedPreferences**/
- Editor editor = mShared.edit();
- editor.putString(KEY_NAME, name);
- editor.putString(KEY_NUMBER, number);
- /**put完毕必需要commit()否则无法保存**/
- editor.commit();
- ShowDialog("保存SharedPreferences成功");
- }
- });
- Button button1 = (Button)findViewById(R.id.sp_button1);
- button1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- /**开始清除SharedPreferences中保存的内容**/
- Editor editor = mShared.edit();
- editor.remove(KEY_NAME);
- editor.remove(KEY_NUMBER);
- //editor.clear();
- editor.commit();
- ShowDialog("清除SharedPreferences数据成功");
- }
- });
- Button button2 = (Button)findViewById(R.id.sp_button2);
- button2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- /** 删除SharedPreferences文件 **/
- File file = new File(DATA_URL + getPackageName().toString()
- + "/shared_prefs", SHARED_MAIN_XML);
- if (file.exists()) {
- file.delete();
- }
- ShowDialog("删除SharedPreferences文件成功");
- }
- });
- super.onCreate(savedInstanceState);
- }
- public void ShowDialog(String string) {
- AlertDialog.Builder builder = new AlertDialog.Builder(SPActivity.this);
- builder.setIcon(R.drawable.icon);
- builder.setTitle(string);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- finish();
- }
- });
- builder.show();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <ImageView android:id="@+id/sp_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/image"
- android:layout_gravity="center"
- />
- <EditText android:id="@+id/sp_et0"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入你的姓名">
- </EditText>
- <EditText android:id="@+id/sp_et1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入你的号码">
- </EditText>
- <Button android:id="@+id/sp_button0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="保存输入内容shared">
- </Button>
- <Button android:id="@+id/sp_button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="清除shared保存内容">
- </Button>
- <Button android:id="@+id/sp_button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="删除shared文件">
- </Button>
- </LinearLayout>