国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android SharedPreferences存儲用法詳解

2019-12-12 03:36:52
字體:
供稿:網(wǎng)友

先看Demo運(yùn)行效果

SharedPreferences詳解

SharedPreferences是Android平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出.

SharedPreferences提供了java常規(guī)的Long、Int、String等類型數(shù)據(jù)的保存接口.

SharedPreferences類似過去Windows系統(tǒng)上的ini配置文件,但是它分為多種權(quán)限,可以全局共享訪問。

提示最終是以xml方式來保存,整體效率來看不是特別的高,對于常規(guī)的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義文件格式。xml處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對于內(nèi)存資源占用比較好.

SharedPreferences數(shù)據(jù)的四種操作模式

Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE

Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會覆蓋原文件的內(nèi)容
Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應(yīng)用是否有權(quán)限讀寫該文件.
MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取.
MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入

SharedPreferences使用步驟

SharedPreferences的使用非常簡單,使用SharedPreferences保存key-value對的步驟如下:

  (1)使用Activity類的getSharedPreferences方法獲得SharedPreferences對象,其中存儲key-value的文件的名稱由getSharedPreferences方法的第一個參數(shù)指定.
  (2)使用SharedPreferences接口的edit獲得SharedPreferences.Editor對象.
  (3)通過SharedPreferences.Editor接口的putXxx方法保存key-value對。其中Xxx表示不同的數(shù)據(jù)類型。例如:字符串類型的value需要用putString方法.
  (4)通過SharedPreferences.Editor接口的commit方法保存key-value對。commit方法相當(dāng)于數(shù)據(jù)庫事務(wù)中的提交(commit)操作.

具體代碼的書寫流程為:

 A、存放數(shù)據(jù)信息

1、打開Preferences,名稱為config,如果存在則打開它,否則創(chuàng)建新的Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、讓config處于編輯狀態(tài)
SharedPreferences.Editor editor =config.edit();
3、存放數(shù)據(jù)
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();

B、讀取數(shù)據(jù)信息
1、獲取Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、取出數(shù)據(jù)
String name =config.getString(“name”,”默認(rèn)值”);
String url = config.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法 

demo的實(shí)現(xiàn)

MainActivity布局文件

<?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:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.duanlian.sharedpreferencesdemo.MainActivity">   <LinearLayout  android:layout_width="match_parent"  android:layout_height="50dp">   <TextView   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:gravity="center"   android:text="用戶名"   android:textSize="23sp" />   <EditText   android:id="@+id/username"   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1" />  </LinearLayout>  <LinearLayout  android:layout_width="match_parent"  android:layout_height="50dp">   <TextView   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:gravity="center"   android:text="密 碼"   android:textSize="23sp" />   <EditText   android:id="@+id/password"   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1" />  </LinearLayout>  <Button  android:onClick="save"  android:layout_width="match_parent"  android:layout_height="50dp"  android:text="保存信息"/>  <Button  android:onClick="change"  android:layout_width="match_parent"  android:layout_height="50dp"  android:text="跳轉(zhuǎn)"/> </LinearLayout> 

ActivityA的布局文件

<?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:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.duanlian.sharedpreferencesdemo.MainActivity">   <LinearLayout  android:layout_width="match_parent"  android:layout_height="50dp">   <TextView   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:gravity="center"   android:text="用戶名"   android:textSize="23sp" />   <EditText   android:id="@+id/username"   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1" />  </LinearLayout>  <LinearLayout  android:layout_width="match_parent"  android:layout_height="50dp">   <TextView   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:gravity="center"   android:text="密 碼"   android:textSize="23sp" />   <EditText   android:id="@+id/password"   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_weight="1" />  </LinearLayout>  <Button  android:onClick="get"  android:layout_width="match_parent"  android:layout_height="50dp"  android:text="得到信息"/> </LinearLayout> 

MainActivity里存儲的具體實(shí)現(xiàn)
都是按照上面寫的步驟來實(shí)現(xiàn)的,最后一定要提交

package com.duanlian.sharedpreferencesdemo;  import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;  public class MainActivity extends AppCompatActivity {   private EditText passWors;  private EditText userName;  private SharedPreferences preferences;  private SharedPreferences.Editor editor;   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  initView();  }   private void initView() {  userName = (EditText) findViewById(R.id.username);  passWors = (EditText) findViewById(R.id.password);  //1,實(shí)例化SharedPreferences對象,參數(shù)1:保存的名字,參數(shù)2:保存的模式MODE_PRIVATE私有的  preferences = getSharedPreferences("config", MODE_PRIVATE);  //2,讓SharedPreferences處于可編輯狀態(tài)  editor = preferences.edit();   }   /**  * 保存按鈕的監(jiān)聽  * @param view  */  public void save(View view) {  //拿到用戶輸入的數(shù)據(jù)  String name = userName.getText().toString().trim();  String pwd = passWors.getText().toString().trim();  //3,儲存數(shù)據(jù),類似于map  editor.putString("name", name);  editor.putString("pwd", pwd);  //4,提交  editor.commit();   }   /**  * 跳轉(zhuǎn)的點(diǎn)擊監(jiān)聽  * @param view  */  public void change(View view) {  Intent intent = new Intent(MainActivity.this, ActivityA.class);  startActivity(intent);   } } 

取出數(shù)據(jù)的實(shí)現(xiàn)

package com.duanlian.sharedpreferencesdemo;  import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;  public class ActivityA extends AppCompatActivity {   private EditText userName;  private EditText passWord;  private String name;  private String pwd;   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_a);  //得到數(shù)據(jù)  //1,得到SharedPreferences對象  SharedPreferences preferences = getSharedPreferences("config", 0);  //2,取出數(shù)據(jù)   name = preferences.getString("name","");   pwd = preferences.getString("pwd", "");  userName = (EditText) findViewById(R.id.username);  passWord = (EditText) findViewById(R.id.password);   }   /**  * 得到數(shù)據(jù)按鈕的監(jiān)聽  * @param view  */  public void get(View view) {  userName.setText(name);  passWord.setText(pwd);  } } 

demo下載地址:SharedPreferences

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 炉霍县| 尚志市| 沁源县| 武冈市| 合阳县| 汉源县| 积石山| 福安市| 富民县| 和林格尔县| 安新县| 太湖县| 乳山市| 惠州市| 尤溪县| 淮南市| 固安县| 航空| 大港区| 新兴县| 林西县| 绥宁县| 砚山县| 延吉市| 馆陶县| 集贤县| 禄劝| 垣曲县| 县级市| 永顺县| 泰宁县| 远安县| 神农架林区| 涞源县| 梅河口市| 阿图什市| 大新县| 健康| 屏东市| 阿城市| 凌云县|