zoukankan      html  css  js  c++  java
  • Android学习(九) SharedPreferences

    一、SharedPreferences:一种清醒的存储方式,基于XML存储key-value键值对方式的数据。

      SharedPreferences对象本身只能获取数据,而不能存储和修改数据,存储修改只能通过Editor对象实现。、

      实现步骤:

      1、获取SharedPreferences对象。

      2、获取SharedPreferences.Editor对象。

      3、通过Editor接口的pubXxx方法保存key-value对,其中Xxx表示不同的数据类型。

      4、通过Editor对象的commit方法提交数据。

      示例:

      main.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" >
    
        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存数据" />
    
        <Button
            android:id="@+id/btn_load"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="获取数据" />
        
        <Button
            android:id="@+id/btn_remove"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="删除数据" />
    
    </LinearLayout>

      main_activity.java

    package com.example.sharedpreferencesdemo1;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity{
    
        Button btn_save;
        Button btn_load;
        Button btn_remove;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            btn_save = (Button) findViewById(R.id.btn_save);
            btn_load = (Button) findViewById(R.id.btn_load);
            btn_remove = (Button) findViewById(R.id.btn_remove);
            
            btn_save.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //获取SharedPreferences对象
                    SharedPreferences pref = getSharedPreferences("mypreferences", MODE_PRIVATE);
                    //获取Editor对象
                    Editor editor = pref.edit();        
                    //写入数据
                    editor.putString("name", "张三");
                    editor.putInt("age", 30);
                    editor.putLong("time", System.currentTimeMillis());
                    editor.putBoolean("state", true);
                    
                    //保存数据
                    editor.commit();
                    
                    //提示
                    Toast.makeText(MainActivity.this, "保存成功", 3).show();
                }
            });
            
            //读取数据
            btn_load.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    //获取SharedPreferences对象
                    SharedPreferences pref = getSharedPreferences("mypreferences", MODE_PRIVATE);
                    //通过getString方法获取值,第一个参数为key值,第二个为未取到值时给的默认值
                    System.out.println(pref.getString("name", ""));
                    System.out.println(pref.getInt("age", 0));
                    System.out.println(pref.getBoolean("state", false));
                    System.out.println(pref.getLong("time", 0));
                }
            });
            
            btn_remove.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //获取SharedPreferences对象
                    SharedPreferences pref = getSharedPreferences("mypreferences", MODE_PRIVATE);
                    //获取editor对象
                    Editor editor = pref.edit();
                    // 通过key值移除数据
                    editor.remove("name");
                    //提交数据
                    editor.commit();
                }
            });
        }
    
    }

      实现保存,读取和删除数据。

  • 相关阅读:
    USB Descriptors
    回车(carriage return : \r) 换行(line feed : \n)
    SQLSERVER改变已有数据表中的列
    SQLSERVER数据库中的5173错误
    SQLSERVER 在局域网使用Windows身份验证连接局域网内的另一台SQL服务器(不是域环境)
    对于索引假脱机的一点理解
    SQLSERVER备份系统数据库以及何时备份系统数据库
    SQL PROMPT5.3.4.1的一些设置选项
    设置SQLSERVER的错误日志数量和查找SQLSERVER安装错误日志
    谈谈我是如何学习SQL Server的
  • 原文地址:https://www.cnblogs.com/zhengcheng/p/4367319.html
Copyright © 2011-2022 走看看