如何在Android項目中是SharedPreferences實現(xiàn)存儲?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、邕寧網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為邕寧等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
SharedPreferences詳解
SharedPreferences是Android平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當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:為默認操作模式,代表該文件是私有數(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:表示當前文件可以被其他應(yīng)用讀取.
MODE_WORLD_WRITEABLE:表示當前文件可以被其他應(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方法相當于數(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”,”默認值”);
String url = config.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法
demo的實現(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里存儲的具體實現(xiàn)
都是按照上面寫的步驟來實現(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,實例化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)的點擊監(jiān)聽
* @param view
*/
public void change(View view) {
Intent intent = new Intent(MainActivity.this, ActivityA.class);
startActivity(intent);
}
}
取出數(shù)據(jù)的實現(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);
}
}
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
新聞名稱:如何在Android項目中是SharedPreferences實現(xiàn)存儲
文章出自:http://chinadenli.net/article0/gsggoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、服務(wù)器托管、關(guān)鍵詞優(yōu)化、網(wǎng)站收錄、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)