zoukankan      html  css  js  c++  java
  • 数据存储--SharedPreferences

    sharedPreferences是通过xml文件来做数据存储的。
    一般用来存放一些标记性的数据,一些设置信息。


    *********使用sharedPreferences存储数据


    1.通过Context对象创建一个SharedPreference对象
    //name:sharedpreference文件的名称 mode:文件的操作模式
    SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
    2.通过sharedPreferences对象获取一个Editor对象
    Editor editor = sharedPreferences.edit();
    3.往Editor中添加数据
    editor.putString("username", username);
    editor.putString("password", password);
    4.提交Editor对象
    editor.commit();

    *********使用sharedPreferences读取数据

    1.通过Context对象创建一个SharedPreference对象
    SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);

    2.通过sharedPreference获取存放的数据
    //key:存放数据时的key defValue: 默认值,根据业务需求来写
    String username = sharedPreferences.getString("username", "");
    String password = sharedPreferences.getString("password", "");


    通过PreferenceManager可以获取一个默认的sharepreferences对象
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

  • 相关阅读:
    Android -- ConditionVariable
    Android -- startActivityForResult和setResult
    StringTokenizer
    Android -- java代码设置margin
    Android -- DisplayMetrics
    Android -- TypedArray
    Android -- 屏幕亮度
    Java assert
    Android -- com.android.providers.media,external.db
    Inno Setup入门(十)——操作注册表
  • 原文地址:https://www.cnblogs.com/wwttsqt/p/6441384.html
Copyright © 2011-2022 走看看