您的位置:首页 > 移动开发 > Android开发

android 数据存储<二>---- SharedPreferences实现数据的存储

2014-05-21 23:11 399 查看
SharedPreferences作为android的存储方式有以下特点:

1.只能存放key-value模式的键值。

2.本质就是就是以xml文件在应用程序所在包中存放数据。(/data/data/xxxx/sharePreferfence/xxx.xml)

3. SharedPreferences 通过操作android的SharedPreferences类来完成xml文件的生成,增,删,改 的动作都由android系统内部模块完成和解析的。用户不需要去

xml文件的生成和解析

4.由于 SharedPreferences 只能存放key-value 简单的数据结构,通过用来做软件配置参数,用来配置用户对软件的自定义或设置参数。

如果要存在复杂的数据,可以使用文件,如果还需要方便的增删改查 的话,就只能用Sqlite数据库来完成

下面是该使用的代码:

所用的字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">SharePreference</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="name">姓名</string>
<string name="ID">工号</string>
<string name="number">电话号码</string>
<string name="save">保存</string>
<string name="Read">读取</string>
</resources>


所用的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_margin="5sp"
/>
<EditText
android:layout_width="170sp"
android:layout_height="40sp"
android:inputType="text"
android:background="#ffffff"
android:gravity="left"
android:layout_margin="5sp"
android:textSize="30sp"
android:id="@+id/text0"

/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ID"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_margin="5sp"
/>
<EditText
android:layout_width="150sp"
android:layout_height="40sp"
android:background="#ffffff"
android:gravity="left"
android:layout_margin="5sp"
android:textSize="30sp"
android:inputType="number"
android:id="@+id/text1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number"
android:textSize="25sp"
android:layout_margin="5sp"
android:textColor="#ffffff"

/>
<EditText
android:layout_width="200sp"
android:layout_height="40sp"
android:inputType="phone"
android:background="#ffffff"
android:gravity="left"
android:textSize="30sp"
android:layout_margin="5sp"

android:id="@+id/text2"

/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:layout_margin="5sp"
android:onClick="save"
android:background="#ffffff"
android:id="@+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Read"
android:layout_margin="5sp"
android:onClick="save"
android:background="#ffffff"
android:id="@+id/button1"
/>
</LinearLayout>
</LinearLayout>


注意这里button控件的android:Onclick 属性,该方法是在XML完成按键的监听注册,并且时间触发处理函数为save,也就是当用户点击这个button ,系统接收到这个事件就会调用

save这个函数。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:background">#000000</item>
</style>

</resources>


为了改变activity的风格,增加喜爱的风格,可以修改这个style.xml, 详见google android 开发者手册

activity 类代码

package com.example.sharepreference;

import java.util.HashMap;
import java.util.Map;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
Button bt,bt1;
EditText text0,text1,text2;
Map<String,String> param=new HashMap<String,String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.button);
bt1=(Button)findViewById(R.id.button1);
text0=(EditText)findViewById(R.id.text0);
text1=(EditText)findViewById(R.id.text1);
text2=(EditText)findViewById(R.id.text2);
SharePref pref2=new SharePref(this.getApplicationContext());
param=pref2.ReadPref();
text0.setText(param.get("name"));
text1.setText(param.get("ID"));
text2.setText(param.get("phone"));
}

public void save(View v)
{
Button bx=(Button)v;
String name;
int ID;
String phone;
switch(bx.getId())
{
case R.id.button:
name=text0.getText().toString();
ID=Integer.valueOf(text1.getText().toString());
phone=text2.getText().toString();
SharePref pref=new SharePref(this.getApplicationContext());
pref.save(name, ID, phone);
Toast.makeText(this.getApplicationContext(), "写入数据成功", Toast.LENGTH_LONG).show();
break;
case R.id.button1:

SharePref pref1=new SharePref(this.getApplicationContext());
param=pref1.ReadPref();
text0.setText(param.get("name"));
text1.setText(param.get("ID"));
text2.setText(param.get("phone"));

}
}

}
采用XML注册按键事件和在代码注册按键事件回调函数实现一样,只是注册方式不一样,这种方式注册更加方便快捷。同时要注意:该类的回调函数必需是public类型,否则外界不能访问。

业务方法的实现:

package com.example.sharepreference;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharePref
{
private static final String prefname="mypref";
private Context context;

public SharePref(Context context)
{

this.context = context;

}

void save (String name,int ID,String phone)
{
SharedPreferences sp= context.getSharedPreferences(prefname, Context.MODE_PRIVATE);
Editor edit= sp.edit();
edit.putString("name", name);
edit.putInt("ID",ID );
edit.putString("phone", phone);
edit.commit();
}
Map<String,String> ReadPref()
{
SharedPreferences sp= context.getSharedPreferences(prefname, Context.MODE_PRIVATE);
Map<String,String> data=new HashMap<String,String>();
data.put("name", sp.getString("name", "zhangxiaosan"));
data.put("ID", String.valueOf(sp.getInt("ID", 10143808)));
data.put("phone", sp.getString("phone", "13787705812"));
return data;
}
}


这里引进了一个Map集合类。可以理解为一个存键值对的数组。或者链表。用户只需要创建一实体,然后想里面添加数据和取出数据,即可

结果如下:

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