android 简单数据存储SharedPreferences

来源:互联网 发布:淘宝助理mac官方版 编辑:程序博客网 时间:2024/04/30 03:52
使用Perference来实现数据的存储,用到了SharedPreferences接口和SharedPreferences内部的一个接口SharedPreferences.Editor。
调用Context.getSharedPreferences(String name,int mode)得到SharedPreferences接口。该方法的第一个参数是文件名称,第二个参数是操作模式,android给我们提供了三种模式:
.私有(MODE_PRIVATE):仅有创建程序有权限对其进行读取或写入
全局读(MODE_WORLD_READABLE):不仅创建程序可以对其进行读取或写入,其他应用程序也读取操作的权限,但没有写入操作的权限
全局写(MODE_WORLD_WRITEABLE):创建程序和其他程序都可以对其进行写入操作,但没有读取的权限
接下来,我们使用一个简单的例子实现上面的功能:
将数据保存到手机的sd中,需要如下的几步:
1.       通过getSharedPreferences(String name,int mode)得到SharedPreferences接口。该方法的第一个参数是文件名称,第二个参数是操作模式
2.       调用SharedPreferences.Editor方法对SharedPreferences进行修改
3.       往editor对象塞值并且提交
实现的代码如下:

Main.xml

<RelativeLayout 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" >    <EditText        android:id="@+id/et_name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="16dp"        android:ems="10"        android:hint="姓名" />    <EditText        android:id="@+id/et_height"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_marginTop="168dp"        android:ems="10"        android:hint="身高" />    <EditText        android:id="@+id/et_age"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/et_name"        android:layout_marginTop="32dp"        android:ems="10"        android:hint="年龄" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/et_height"        android:layout_marginLeft="32dp"        android:layout_marginTop="40dp"        android:gravity="center_horizontal"        android:text="Preferences的简单的测试" /></RelativeLayout>

Mainactivity.java

package com.example.perference;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {       EditText et_name;       EditText et_age;;       EditText et_height;       Button btn;             public static int MODE = MODE_PRIVATE;//定义访问模式为私有模式       public static final String PREFERENCE_NAME = "saveInfo";//设置保存时的文件的名称    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                et_name=  (EditText)this.findViewById(R.id.et_name);       et_age=(EditText)this.findViewById(R.id.et_age);       et_height=(EditText)this.findViewById(R.id.et_height);             btn=(Button)this.findViewById(R.id.button1);       btn.setOnClickListener(this);                        }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }       public void onClick(View v) {              //下面的代码实现的是保存数据              SharedPreferences sharedpreferences=this.getSharedPreferences(PREFERENCE_NAME, MODE);//通过getSharedPreferences(String name,int mode)得到SharedPreferences接口。该方法的第一个参数是文件名称,第二个参数是操作模式              SharedPreferences.Editor editor=sharedpreferences.edit();//调用SharedPreferences.Editor方法对SharedPreferences进行修改                       String name=et_name.getText().toString();              int age=Integer.parseInt(et_age.getText().toString());              Float height=Float.parseFloat(et_height.getText().toString());                            editor.putString("Name", name);//往editor对象塞值              editor.putInt("Age", age);              editor.putFloat("Height", height);              editor.commit();              Toast.makeText(this, "我已经保存了", Toast.LENGTH_LONG).show();                            }}


生成的xml文件通常情况下在手机的/data/data/<package name>/shared_prefs/xxx.xml的目录下,有些手机没有进去data文件夹的权限,需要破解。

原文地址:http://www.apkbus.com/android-83320-1-1.html


原创粉丝点击