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

首頁 > 學院 > 開發設計 > 正文

SharedPreferences的用法以及詳解

2019-11-06 09:45:23
字體:
來源:轉載
供稿:網友

用了很多次的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的基本使用方法相信你們有所了解,如果有錯誤之處歡迎指正。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸西县| 柳林县| 通辽市| 阳朔县| 汽车| 忻州市| 乐业县| 邢台市| 咸丰县| 南汇区| 镇安县| 怀化市| 博罗县| 丹江口市| 安阳市| 镇赉县| 耿马| 增城市| 营口市| 平罗县| 旺苍县| 张家港市| 镇远县| 柳河县| 家居| 宁武县| 大宁县| 冀州市| 晋州市| 泽州县| 山西省| 沁水县| 会东县| 永兴县| 易门县| 繁峙县| 壶关县| 全南县| 宝兴县| 申扎县| 汨罗市|