Android:SharedPreferences解析和實現記住用戶名

SharedPreferences

    SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些經常使用的配置好比窗口狀態,它提供了Android平臺常規的Long長整形、Int整形、String字符串型的保存。SharedPreferences不支持多線程。例如,能夠經過它保存上一次用戶所作的修改或者自定義參數設定,當再次啓動程序後依然保持原有的設置。 html

    另外的數據存儲方式還有SQLite、Content Provider、File... java

    用法:

        SharedPreferences對象自己只能獲取數據而不支持存儲和修改,存儲修改是經過Editor對象實現的。 android

        存放: git

        1.得到SharedPreferences 的實例對象,經過getSharedPreferences()傳遞存儲時的名稱和模式 多線程

        2.得到Editor 的實例對象,經過SharedPreferences 的實例對象的edit() 編輯器

        3.存入數據用Editor的putXXX()  [存放什麼數據就put那個數據的類型,支持Long、Int、String、Boolean] ide

        4.提交修改的數據用Editor的commit() 佈局

        讀取: spa

        1.得到SharedPreferences 的實例對象,經過getSharedPreferences()傳遞存儲時的名稱和模式 線程

        2.讀取數據,經過SharedPreferences 的實例對象的getXXX()       [讀取什麼類型的數據就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);

    一、數據共享

        2個activity 之間能夠使用SharedPreferences來共享數據的方式

    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);

    二、保存修改

    這裏用記住用戶名爲例子解說

    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>
    java代碼
public class DemoActivity extends Activity {
	
	public static final String PREFS_NAME = "prefsname"; //偏好設置名稱
	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()); //設置用戶名
		
		//保存用戶名
		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(); //保存數據
		//editor.clear();//清除數據
	}

	// 設置是否保存的用戶名
	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);
	}
}

   頁面

    


下面分享熊貓82 的SQLite、Content Provider、File的博文:

Android數據的四種存儲方式SharedPreferences、SQLite、Content Provider和File (一) —— 總覽

Android數據的四種存儲方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite

Android數據的四種存儲方式SharedPreferences、SQLite、Content Provider和File (三) —— SharedPreferences

Android數據的四種存儲方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

相關文章
相關標籤/搜索