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

android学习笔记之sharedpreferencess

2016-06-05 21:06 363 查看
sharedpreference是安卓提供的一种轻量级的存储手段,不同于文件操作和SQlite,它主要用于软件的配置参数,或一些简单的数据存储,例如,天气预报的默认城市。

通过我编写的一段代码进行解释

package com.example.administrator.sharedpreferences;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView textView;
Button btn_save,btn_get;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.tv);
btn_get= (Button) findViewById(R.id.btn_get);
btn_save= (Button) findViewById(R.id.btn_save);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

sharedPreferences=getSharedPreferences("mydata",MODE_APPEND);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putString("name","zeng");
editor.putString("sex","man");
editor.putInt("age",22);
editor.commit();
}
});
btn_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedPreferences=getSharedPreferences("mydata",MODE_APPEND);
String name=sharedPreferences.getString("name","null");
String sex=sharedPreferences.getString("sex","null");
int age=sharedPreferences.getInt("age",0);
textView.setText(name+sex+age);
}
});
}
}
1.首先定义了一个sharedpreference,通过getsharedpreference方法获得实例,参数有两个第一个是Preference的名字,第二个是操作方式:

google官方文档说明:使用0或者MODE_PRIVATE是默认操作方式,也就是说自能自己使用,MODE_WORLD_READABLE和MODE_WORLD_WERTABLE来控制访问权限

代码中我使用的是MODE_APPEND,是追加模式,存入得数据不会被清除,新的会追加在后面,默认的模式是会重写以前存下的。

2.通过SharedPreferences.edit()方法得到编辑对象(SharedPreference.Editor),editor的操作和map的方式类似,使用put方法以键值的方式存

3.使用commit()方法提交并存入xml文件

我在布局文件中定义一个button并设置事件将存入xml的信息显示在textview中

布局文件很简单,

<?xml version="1.0" encoding="utf-8"?>
<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"
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.example.administrator.sharedpreferences.MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
<Button
android:layout_below="@id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="存入"
android:id="@+id/btn_save"/>
<Button
android:layout_below="@id/btn_save"
android:text="取出"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_get"/>
</RelativeLayout>
运行效果如下



在目录/data/data/com.example.administrator.share_prefs下存在我们所创建的preference文件

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