用了很多次的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的基本使用方法相信你们有所了解,如果有错误之处欢迎指正。
新闻热点
疑难解答