zoukankan      html  css  js  c++  java
  • 数据存储——SP存储 SP读取

    一.存储方式分类:SharedPreferences存储

    二.SharedPreferences存储

    1.特点

    ①存储单一数据,例如数值,字符串,布尔

    ②文件:/date/date/包名/shared_prefs/xxx.xml:    <map><string  name="key">value</string></map>

    ③以键值对的形式存储

    ④可以设置不被其他应用操作

    2.API

    (1)SharedPreferences

    ①获取实例context.getSharedPreferences():

    1)name  存储文件名;  2)mode  操作模式:MODE_PRIVATE不能被别的应用访问,覆盖模式;MODE_APPEND不能被别的应用访问,追加模式;

    ②启动编辑器:edit():返回Editor

    ③读取Value:1)getString(key,defValue  缺省值)  2)getAll( )  返回所有键值对的 Map集合

    (2)Editor

    ①存放数据:1)putString(key,value)  2)putLong(key,value)  3)putInt  4)putFloat  5)putBoolean  6)putStringSet

    ②提交存储

    ③clear()  清除

    ④remove(String  key)  移除指令key的键值对

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp3.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <TextView
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="Hello World!" />
    17     
    18     <Button
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:text="SP存储"
    22         android:onClick="bt_OnClick"/>
    23 
    24     <Button
    25         android:layout_width="match_parent"
    26         android:layout_height="wrap_content"
    27         android:text="SP读取"
    28         android:onClick="bt1_OnClick"/>
    29 
    30 </LinearLayout>
    .xml
     1 package com.hanqi.testapp3;
     2 
     3 import android.content.SharedPreferences;
     4 import android.os.Bundle;
     5 import android.support.v7.app.AppCompatActivity;
     6 import android.view.View;
     7 import android.widget.Toast;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_main);
    15     }
    16 
    17     public void bt_OnClick(View v)
    18     {
    19         //1.得到SharedPreferences对象
    20         SharedPreferences sharedPreferences=getSharedPreferences("abc",MODE_APPEND);
    21 
    22         //2.得到编辑器
    23         SharedPreferences.Editor editor=sharedPreferences.edit();
    24 
    25         //3.使用editor添加数据
    26 //        editor.putString("b","xxxxxx");
    27 //        editor.putLong("long",123456);
    28 
    29         editor.remove("a");
    30 
    31 
    32         //4.提交保存
    33         editor.commit();
    34 
    35         Toast.makeText(MainActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
    36     }
    37 
    38     //读取
    39     public void bt1_OnClick(View v)
    40     {
    41         SharedPreferences sp=getSharedPreferences("abc",MODE_PRIVATE);
    42 
    43         String str=sp.getString("ab", "默认值");
    44 
    45         Toast.makeText(MainActivity.this, "key=b"+"value="+str, Toast.LENGTH_SHORT).show();
    46     }
    47 }
    .Java

      

       

  • 相关阅读:
    java知识点-高级
    Java中高级面试题
    项目基础
    TFS Build Error: CSC : fatal error CS0042: Unexpected error creating debug information file 'xxxx.PDB'
    System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
    (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
    (C#) 引用工程中发现有黄色叹号
    (C#).NET 2.0 ~ 4.0 OS requirements.
    (WCF) WCF and Service Debug
    (WCF) WCF Service Hosting.
  • 原文地址:https://www.cnblogs.com/cycanfly/p/5521299.html
Copyright © 2011-2022 走看看