前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android 数据存储<二>---- SharedPreferences实现数据的存储

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

作者头像
用户4148957
发布2022-06-14 08:25:54
5K0
发布2022-06-14 08:25:54
举报
文章被收录于专栏:C/C++与音视频C/C++与音视频

 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数据库来完成

下面是该使用的代码:

所用的字符串

代码语言:javascript
复制
<?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>

所用的布局文件:

代码语言:javascript
复制
<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这个函数。

代码语言:javascript
复制
<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 类代码

代码语言:javascript
复制
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类型,否则外界不能访问。 业务方法的实现:

代码语言:javascript
复制
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集合类。可以理解为一个存键值对的数组。或者链表。用户只需要创建一实体,然后想里面添加数据和取出数据,即可

结果如下:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档