android学习笔记之sharedpreferencess

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文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值