SharedPreferences解析和實現(xiàn)記住用戶名
SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態(tài),它提供了Android平臺常規(guī)的Long長整形、Int整形、String字符串型的保存。
SharedPreferences不支持多線程。例如,可以通過它保存上一次用戶所做的修改或者自定義參數(shù)設(shè)定,當再次啟動程序后依然保持原有的設(shè)置。
另外的數(shù)據(jù)存儲方式還有SQLite、Content Provider、File...
用法:
SharedPreferences對象本身只能獲取數(shù)據(jù)而不支持存儲和修改,存儲修改是通過Editor對象實現(xiàn)的。
存放:
1.獲得SharedPreferences 的實例對象,通過getSharedPreferences()傳遞存儲時的名稱和模式
2.獲得Editor 的實例對象,通過SharedPreferences 的實例對象的edit()
3.存入數(shù)據(jù)用Editor的putXXX() [存放什么數(shù)據(jù)就put那個數(shù)據(jù)的類型,支持Long、Int、String、Boolean]
4.提交修改的數(shù)據(jù)用Editor的commit()
讀?。?/p>
1.獲得SharedPreferences 的實例對象,通過getSharedPreferences()傳遞存儲時的名稱和模式
2.讀取數(shù)據(jù),通過SharedPreferences 的實例對象的getXXX() [讀取什么類型的數(shù)據(jù)就get那個類型]
getSharedPreferences方法擴展
getSharedPreferences("存儲時的名稱","模式")
模式(可組合使用):
私有: Context.MODE_PRIVATE 值0
公開可讀:Context.MODE_WORLD_READABLE 值1
公開可寫:Context.MODE_WORLD_WRITEABLE 值2
- SharedPreferences sp = getSharedPreferences("mydata", Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_WRITEABLE);
 
1、數(shù)據(jù)共享
2個activity 之間可以使用SharedPreferences來共享數(shù)據(jù)的方式
A類
- Editor sharedata = getSharedPreferences("data", 0).edit();
 - sharedata.putString("item","hello getSharedPreferences");
 - sharedata.commit();
 
B類
- SharedPreferences sharedata = getSharedPreferences("data", 0);
 - String data = sharedata.getString("item", null);
 - Log.v("cola","data="+data);
 
2、保存修改
這里用記住用戶名為例子解說
main.xml 布局文件
- <?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" >
 - <LinearLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - <TextView
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="用戶名:" />
 - <EditText
 - android:id="@+id/login_user_et"
 - android:layout_width="150dip"
 - android:layout_height="wrap_content"
 - android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
 - </LinearLayout>
 - <LinearLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - <TextView
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="密 碼:" />
 - <EditText
 - android:id="@+id/login_pswd_et"
 - android:layout_width="150dip"
 - android:layout_height="wrap_content"
 - android:password="true"/>
 - </LinearLayout>
 - <LinearLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:orientation="horizontal">
 - <TextView
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="記住密碼:" />
 - <CheckBox
 - android:id="@+id/login_checkbox"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"/>
 - </LinearLayout>
 - <Button
 - android:id="@+id/login_btn"
 - android:layout_width="200dip"
 - android:layout_height="wrap_content"
 - android:text="登陸"/>
 - </LinearLayout>
 
- public class DemoActivity extends Activity {
 - public static final String PREFS_NAME = "prefsname"; //偏好設(shè)置名稱
 - public static final String REMEMBER_USERID_KEY = "remember"; //記住用戶名
 - public static final String USERID_KEY = "userid"; //用戶名標記
 - private static final String DEFAULT_USERNAME = "Hades"; //默認用戶名
 - //組件
 - private EditText userName = null;
 - private EditText passWord = null;
 - private CheckBox cb = null;
 - private SharedPreferences mSettings = null;
 - private Button submitBtn = null;
 - @Override
 - public void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.main);
 - userName = (EditText) findViewById(R.id.login_user_et);
 - passWord = (EditText) findViewById(R.id.login_pswd_et);
 - cb = (CheckBox) findViewById(R.id.login_checkbox);
 - submitBtn = (Button) findViewById(R.id.login_btn);
 - mSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //模式為私有
 - cb.setChecked(getRemember()); //勾選記住用戶名
 - userName.setText(getUserName()); //設(shè)置用戶名
 - //保存用戶名
 - submitBtn.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View v) {
 - //是否保存用戶名
 - if (cb.isChecked()) {
 - saveRemember(true);
 - saveUserName(userName.getText().toString());
 - } else {
 - saveRemember(false);
 - saveUserName("");
 - }
 - }
 - });
 - }
 - // 保存用戶名
 - private void saveUserName(String userid) {
 - Editor editor = mSettings.edit();// 獲取編輯器
 - editor.putString(USERID_KEY, userid);
 - editor.commit(); //保存數(shù)據(jù)
 - //editor.clear();//清除數(shù)據(jù)
 - }
 - // 設(shè)置是否保存的用戶名
 - private void saveRemember(boolean remember) {
 - Editor editor = mSettings.edit();// 獲取編輯器
 - editor.putBoolean(REMEMBER_USERID_KEY, remember);
 - editor.commit();
 - }
 - // 獲取保存的用戶名
 - private String getUserName() {
 - return mSettings.getString(USERID_KEY, DEFAULT_USERNAME);
 - }
 - // 獲取是否保存的用戶名
 - private boolean getRemember() {
 - return mSettings.getBoolean(REMEMBER_USERID_KEY, true);
 - }
 - }
 
頁面:
  















 
 
 


 
 
 
 