您的位置:首页 > Web前端

《第一行代码》第六章 SharedPreferences存储Test

2016-04-11 20:43 357 查看
编者按:刚开始学习安卓,比较基础。从头到尾看郭神的书,一开始也是什么的都不会,比较着急。很多的方法什么的,代码什么的,import过程什么的,老是出错。然而回过头来看看,原来这么简单!很多人都是从这本书开始安卓生涯的。别说我,吻我,因为我只是代码的搬运工!

参考:《第一行代码》

SharedPreferences存储为Sharedpreferences文件,首先是怎么进行存储,然后是怎么进行数据取出。

存储的时候我们首先拿到SharedPreferences对象,三种方式。

Context类的getSharedPreferences(a,b),,其中a为String型的包名(不包含路径,路径是默认存储的);b是操作模式,有两种(其他的已经废除),一种是MODE_PRIVATE(默认),一种是MODE_PROCESS(多个进程对同一文件进行读写的情况)。

Activity中的getPreferences(a)方法,其中a只是操作模式参数,自动将当前类名作为SharedPreferences的文件名。

PreferenceManager类中的getDefaultSharedPreference(a)方法,这是静态方法,只是接受Context参数,自动使用报名为文件名

哪个相对简单就用哪个,第一种相对比较麻烦,因为在读文件时,你还要输入给定的文件名进行匹配,还有就是三种方式不能交叉使用,因为各自生成的文件名不同。读出时找不到文件。这里交叉使用的意思是:存储和读出文件内容时使用的方法不一样。

拿到SharedPreferences对象之后,调用edit()方法,拿到Editor对象,并使用该对象的各种put()方法重载,用键值对的方式存储数据。

文件内容读出时,只需拿到SharedPreferences对象就可以了,调用对应put重载的get重载方法取出内容。

这里实现和文件存储一样的功能:

java:

package com.example.databasetest;

import android.app.Activity;
import android.app.Fragment.SavedState;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText edt1, edt2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText) findViewById(R.id.edt_1);
edt2 = (EditText) findViewById(R.id.edt_2);
String inputString = load();//加载文件内容
if (!TextUtils.isEmpty(inputString)) {//判断是否为空,不为空则直接显示文件中加载的内容,为空没有逻辑,本身就是空白
edt1.setText(inputString);
edt1.setSelection(inputString.length());
Toast.makeText(this, "You succeed in SharedPreference",
Toast.LENGTH_SHORT).show();
}
edt2.setText(edt2.getText());
}

@Override
protected void onDestroy() {
super.onDestroy();
save();//销毁活动的时刻进行存储
}

public void save() {//存储数据逻辑
String edt1String = edt1.getText().toString();
SharedPreferences preferences = getSharedPreferences("data1",
MODE_PRIVATE);
Editor preEditor = preferences.edit();
preEditor.putString("content", edt1String);
preEditor.commit();
}

public String load() {//加载数据逻辑
SharedPreferences pref=getSharedPreferences("data1", MODE_PRIVATE);
String str = pref.getString("content", "");
return str;
}
}


布局文件

<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" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/sharedpreferences" />

<EditText
android:id="@+id/edt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/type_something_"
android:singleLine="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:text="@string/no_sharedpreferences" />

<EditText
android:id="@+id/edt_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/type_something_"
android:singleLine="true" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: