Android采用SharedPreferences保存数据

本文介绍如何使用SharedPreferences在Android应用中进行简单数据的存储与读取。通过一个示例程序,展示了如何利用SharedPreferences及其Editor对象实现基本类型数据的写入与读取。

使用SharedPreferences在程序的数据空间中生成xml文档来保存数据

package com.hu.data;
 
 import android.app.Activity;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 
 public class ShDemoActivity extends Activity {
    
     private EditText etName,etAge,etScore;
     private Button btWrite,btRead;
     private SharedPreferences sharedPrefrences;
     private Editor editor;
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
        
         etName = (EditText) findViewById(R.id.editTextName);//得到控件
         etAge = (EditText) findViewById(R.id.editTextAge);
         etScore = (EditText) findViewById(R.id.editTextScore);
         btWrite = (Button) findViewById(R.id.buttonWrite);
         btRead = (Button) findViewById(R.id.buttonRead);
        
         sharedPrefrences = this.getSharedPreferences("user", MODE_WORLD_READABLE);//得到SharedPreferences,会生成user.xml
         editor = sharedPrefrences.edit();
        
         btWrite.setOnClickListener(new OnClickListener() {//写入按钮事件
            
             public void onClick(View arg0) {
                 String name = etName.getText().toString();
                 int age = Integer.parseInt(etAge.getText().toString());
                 float score = Float.parseFloat(etScore.getText().toString());//获取用户输入数据
                 editor.putString("name", name);
                 editor.putInt("age", age);
                 editor.putFloat("score", score);//将数据写入xml
                 editor.commit();//提交
             }
         });
        
         btRead.setOnClickListener(new OnClickListener() {//读出按钮事件
            
             public void onClick(View v) {
                 String name = sharedPrefrences.getString("name", null);
                 int age = sharedPrefrences.getInt("age", 0);
                 float score = sharedPrefrences.getFloat("score", 60.0f);//将数据读出
                 etName.setText(name);
                 etAge.setText(Integer.toString(age));
                 etScore.setText(Float.toString(score));//显示数据
             }
         });
        
     }
 }

布局文件为:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
 
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
 
         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="姓名:"
             android:textAppearance="?android:attr/textAppearanceLarge" />
 
         <EditText
             android:id="@+id/editTextName"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1" >
 
             <requestFocus />
         </EditText>
     </LinearLayout>
 
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
 
         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="年龄:"
             android:textAppearance="?android:attr/textAppearanceLarge" />
 
         <EditText
             android:id="@+id/editTextAge"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1" />
     </LinearLayout>
 
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
 
         <TextView
             android:id="@+id/textView3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="分数:"
             android:textAppearance="?android:attr/textAppearanceLarge" />
 
         <EditText
             android:id="@+id/editTextScore"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1" />
     </LinearLayout>
 
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
 
         <Button
             android:id="@+id/buttonWrite"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="写入" />
 
         <Button
             android:id="@+id/buttonRead"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="读出" />
     </LinearLayout>
 
 </LinearLayout>

SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。

转载于:https://www.cnblogs.com/habby/archive/2013/03/18/2966823.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值