android 数据存储一SharedPrenference存储简单数据

使用SharedPrenference 存储数据类似ios 的偏好设置存储数据

适用范围:保存少量的数据,且这些数据的格式非常简单:字符串类型,基本类型的值。比如应用程序的各种配置信息(如是否打开音效、是否使用震动效果、小游戏的玩家积分等),解锁口令密码等。

核心原理:保存基于XML文件存储的key-value键值对数据,通常用来存储一些简单的配置信息。通过DDMS 的file explorer面板,展开文件浏览树,很明显sharepreferences数据总是存储在/data/data/<package name>/shared_prefs目录下,sharedpreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过sharepreferences.edit(),sharepreferences本身是一个接口,程序无法直接创建sharedpreference实例,只能通过contenxt提供的getsharedprenferences(string name,int mode)方法来获取sharedprenferences实例,该方法中的name表示要操作的xml文件名,Editor有如下主要重要方法:

                 SharedPreferences.Editor clear():清空SharedPreferences里所有数据

                 SharedPreferences.Editor putXxx(String key , xxx value):SharedPreferences存入指定key对应的数据,其中xxx可以是boolean,float,int等各种基本类型据

                 SharedPreferences.Editor remove():删除SharedPreferences中指定key对应的数据项

                 boolean commit(): Editor编辑完成后,使用该方法提交修改


示例:直接上代码哈,xml文件

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.data.cn.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入密码口令" />

    <EditText 
        android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/btnset"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="设置解锁口令"
        />
    <Button 
        android:id="@+id/btnget"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取解锁口令"
        />
    
</LinearLayout>
mainactivity中代码:

package com.data.cn;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {

	private Button setBtn;
	private Button getBtn;
	private EditText edText;
	private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = getBaseContext();
		
		setBtn = (Button) findViewById(R.id.btnset);
		getBtn = (Button) findViewById(R.id.btnget);
		edText = (EditText) findViewById(R.id.et);
		
		setBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			String password = edText.getText().toString().trim();
			
			SharedPreferences.Editor editor = getSharedPreferences("lock", context.MODE_PRIVATE).edit();
			
			editor.putString("password", password);
			
			editor.commit();
			
				
			}
		});
		
		
		getBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				SharedPreferences read = getSharedPreferences("lock", context.MODE_PRIVATE);
				String password = read.getString("password", "");
				
				Toast.makeText(context, password, 12000).show();
				
			}
		});
		
		
		
		
	}
	
	
}


运行结果:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值