数据篇—SharedPreferences
Android四种存储方式:
1.SharedPreferences
2.SQLite
3.Content Provider
4.File
SharedPreferences的使用介绍:
1)一种轻型的数据存储方式
2)本质:基于XML文件存储key-value键值对数据
3)常用来存储一些简单的配置信息
1.SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现
2.实现SharedPreferences存储的步骤如下:
1)获得SharedPreferences对象
2)获得SharedPreferences.Editor对象(因为SharedPreferencesd对象不能直接进行存储,存储实现通过Editor对象实现)
3)通过Editor接口的putXxx方法保存key-value对,其中的Xxx表示不同的数据类型
4)通过Editor接口的commit方法保存key-value对
ps:Android默认的XML解析器为DOM
1-2 使用SharedPreferences存取数据
MainActivity.java
package com.example.android3_sharedpreferences; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SharedPreferences preferences=getSharedPreferences("myPreF",MODE_PRIVATE); Editor editor=preferences.edit(); editor.putString("name", "张三"); editor.putInt("age", 17); editor.putLong("time", System.currentTimeMillis()); editor.putBoolean("default",true); editor.commit(); editor.remove("default"); editor.commit(); System.out.println(preferences.getString("name", "")); System.out.println(preferences.getInt("age", 0)); } }
1-3 存取用户名实例
package com.example.android3_sharedpreferences; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText etUserName, etUserPass; CheckBox chk; SharedPreferences pref; Editor edtior; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // // SharedPreferences pref = // PreferenceManager.getDefaultSharedPreferences(MainActivity.this); // SharedPreferences pref =getSharedPreferences("myPref", MODE_PRIVATE); // Editor editor = pref.edit();
//存储数据
// editor.putString("name","张三"); // editor.putInt("age", 30); // editor.putLong("time", System.currentTimeMillis()); // editor.putBoolean("default", true); // editor.commit(); // editor.remove("default"); // editor.commit();//如果这个不写的话,所有操作都无效
// System.out.println(pref.getString("name", "")); // System.out.println(pref.getInt("age", 0)); etUserName = (EditText) findViewById(R.id.etuserName); etUserPass = (EditText) findViewById(R.id.etuserpass); chk = (CheckBox) findViewById(R.id.chkSaveName);
/**
* 第一个参数:文件名.xml
* 第二个参数:模式;如MODE_PRIVATE:只有这个APP可以调用此对象sp
* /
pref = getSharedPreferences("UserInfo", MODE_PRIVATE); edtior = pref.edit(); String name = pref.getString("userName", ""); if (name == null) { chk.setChecked(false); } else { chk.setChecked(true); etUserName.setText(name); } } // 為登錄、取消按鈕添加屬性:(添加點擊事件的另一個方法) public void doClick(View v) { switch (v.getId()) { case R.id.btnLogin: String name = etUserName.getText().toString().trim(); String pass = etUserPass.getText().toString().trim(); if ("admin".equals(name) && "123456".equals(pass)) { if (chk.isChecked()) { edtior.putString("userName", name); edtior.commit(); } else { edtior.remove("userName"); edtior.commit(); } Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(MainActivity.this, "禁止登陆", Toast.LENGTH_LONG) .show(); } break; default: break; } } }
<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="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="match_parent" android:gravity="center" android:layout_weight="1" android:text="用户名:" /> <EditText android:id="@+id/etuserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:orientation="horizontal" > <TextView android:id="@+id/aa" android:layout_width="0dp" android:layout_height="match_parent" android:gravity="center" android:layout_weight="1" android:text="密 码:" /> <EditText android:id="@+id/etuserpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etuserName" android:layout_alignParentRight="true" android:layout_alignTop="@+id/aa" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <CheckBox android:id="@+id/chkSaveName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:checked="false" android:text="保存用户名" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:orientation="horizontal" > <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:layout_marginRight="8dp" android:layout_weight="1" android:text="登陆" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:layout_marginLeft="8dp" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
第一种方法
//SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
第二种方法
*//**
* 第一个参数:文件名.xml
* 第二个参数:模式;如MODE_PRIVATE:只有这个APP可以调用此对象sp
*//*
SharedPreferences sp=getSharedPreferences("myPref", MODE_PRIVATE);
Editor editor=sp.edit();
//存储数据
editor.putString("Name", "慕课网");
editor.putInt("Age", 30);
editor.putLong("Time", System.currentTimeMillis());
editor.putBoolean("Default", true);
editor.commit();//如果这个不写的话,所有操作都无效
editor.remove("Default");
editor.commit();
//读取数据
Log.i("TAG", sp.getString("Name", "默认值"));//第二个参数:取不到数据的默认值
Log.i("TAG", String.valueOf(sp.getInt("Age", 0)));
Log.i("TAG", String.valueOf(sp.getInt("Age1", 0)));//假设取不到
Ps:Eclipse中,Error提示,双击错误地方即可锁定错误的位置。
创建对象默认和自定义
默认:PreferebceManager.getDefauleSharedPreferences(上下文);
自定义:getSharedPreferences("自定义名称",权限)
Editor editor = pref.edit();//编辑器对象
editor.commit();//提交,
editor.remove(key)//移除
ref.getString(key)//取出