用了很多次的SharedPReferences,這次來好好總結一下SharePreferences的用法以及需要了解的知識。主要是寫給我的一位新粉絲看的(然而也只有兩個)。希望以后的粉絲越來越多,我也更加有動力寫更多更好的博客。     首先介紹一下SharedPreference。     SharedPreferences是Android平臺上一個輕量級的存儲類,用來保存應用的一些常用配置,比如Activity狀態,Activity暫停時,將此activity的狀態保存到SharedPereferences中;當Activity重載,系統回調方法onSaveInstanceState時,再從SharedPreferences中將值取出。 其中的原理是通過Android系統生成一個xml文件保到:/data/data/包名/shared_prefs目錄下,類似鍵值對的方式來存儲數據。         Sharedpreferences提供了常規的數據類型保存接口比如:int、long、boolean、String、Float、Set和Map這些數據類型。     好了下面給出一個基本使用的案例,一看應該就明白了下面是演示的效果:     
下面是Activity中的代碼: 主要是實現SharedPreferences值的存取的功能。通過點擊Button1獲取edittext1的數據存入到sharedpreferences中,然后點擊button2將數據取出放入edittext2中
package demo.liuchen.com.welcomepager;import android.content.Context;import android.content.SharedPreferences;import android.icu.text.BreakIterator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class SharePreferenceActivity extends AppCompatActivity { private EditText meditText1 ,meditText2 ; private Button SaveBtn,GetBtn; //聲明Sharedpreferenced對象 private SharedPreferences sp ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share_preference); meditText1= (EditText) findViewById(R.id.edit1); meditText2 = (EditText) findViewById(R.id.edit2); SaveBtn = (Button) findViewById(R.id.btn_Save); GetBtn = (Button) findViewById(R.id.btn_Get); } public void Click(View view) { /** * 獲取SharedPreferenced對象 * 第一個參數是生成xml的文件名 * 第二個參數是存儲的格式(**注意**本文后面會講解) */ sp = getSharedPreferences("User", Context.MODE_PRIVATE); switch (view.getId()){ case R.id.btn_Save: //獲取到edit對象 SharedPreferences.Editor edit = sp.edit(); //通過editor對象寫入數據 edit.putString("Value",meditText1.getText().toString().trim()); //提交數據存入到xml文件中 edit.commit(); break; case R.id.btn_Get: //取出數據,第一個參數是寫入是的鍵,第二個參數是如果沒有獲取到數據就默認返回的值。 String value = sp.getString("Value","Null"); meditText2.setText(value); break; } }}下面是xml布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_share_preference" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="demo.liuchen.com.welcomepager.SharePreferenceActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edit1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="請輸入需要的內容"/> <Button android:layout_weight="1" android:id="@+id/btn_Save" android:layout_width="0dp" android:layout_height="wrap_content" android:text="存入到sharepreference" android:layout_marginLeft="5dp" android:textSize="10dp" android:onClick="Click"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edit2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="請輸入需要的內容"/> <Button android:layout_weight="1" android:id="@+id/btn_Get" android:layout_width="0dp" android:layout_height="wrap_content" android:text="從sharepreference取出" android:layout_marginLeft="5dp" android:onClick="Click" android:textSize="10dp"/> </LinearLayout></LinearLayout>需要注意:getSharedPreferences(“User”, Context.MODE_PRIVATE)方法中第二個參數需要了解Android的四種枚舉方式下面是詳細的解釋: 私有模式 Context.MODE_PRIVATE 的值是 0; ①只能被創建這個文件的當前應用訪問 ②若文件不存在會創建文件;若創建的文件已存在則會覆蓋掉原來的文件
追加模式 Context.MODE_APPEND 的值是 32768; ①只能被創建這個文件的當前應用訪問 ②若文件不存在會創建文件;若文件存在則在文件的末尾進行追加內容
可讀模式 Context.MODE_WORLD_READABLE的值是1; ①創建出來的文件可以被其他應用所讀取
可寫模式 Context.MODE_WORLD_WRITEABLE的值是2 ①允許其他應用對其進行寫入。
好了SharedPreferenced的基本使用方法相信你們有所了解,如果有錯誤之處歡迎指正。
新聞熱點
疑難解答