微餐饮网站建设平台,免费源码分享平台,官方网站后台图片下载怎么做,秦皇岛网站制作 微商城建设一、SharedPreferences 框架体系
1、SharedPreferences 基本介绍 SharedPreferences 是 Android 的一个轻量级存储工具#xff0c;它采用 key - value 的键值对方式进行存储 它允许保存和读取应用中的基本数据类型#xff0c;例如#xff0c;String、int、float、boolean …一、SharedPreferences 框架体系
1、SharedPreferences 基本介绍 SharedPreferences 是 Android 的一个轻量级存储工具它采用 key - value 的键值对方式进行存储 它允许保存和读取应用中的基本数据类型例如String、int、float、boolean 等 保存共享参数键值对信息的文件路径为/data/data/【应用包名】/shared_prefs/【SharedPreferences 文件名】.xml
2、SharedPreferences 使用步骤
1获取 SharedPreferences 实例 其中fileName 是为 SharedPreferences 文件指定的名称 mode 是文件的操作模式通常是 MODE_PRIVATE私有模式
SharedPreferences sharedPreferences context.getSharedPreferences(【fileName】, 【mode】);2写入数据 使用 SharedPreferences.Editor 来编辑数据通过 SharedPreferences 实例的 edit 方法获取 Editor 对象 然后使用 put 相关方法来添加或修改数据当 key - value 不存在时为添加当 key - value 存在时为修改 最后调用 commit 方法来提交更改
SharedPreferences.Editor edit sharedPreferences.edit();
edit.putString(【key】, 【value】);
edit.commit();3读取数据 通过 SharedPreferences 实例的 get 相关方法来读取数据 如果 key 不存在则返回 defValue 默认值
sharedPreferences.getString(【key】, 【defValue】);3、SharedPreferences 使用优化思路 在使用 SharedPreferences 时我们往往只关注一存一取即我们往往只关注【key】和【value】 我们往往不关注【context】、【fileName】、【mode】、【defValue】在使用一一指定这些感觉过于繁琐
4、SharedPreferences 框架体系
使用 SharedPreferences 框架体系可以优化 SharedPreferences 的使用增强 SharedPreferences 相关业务代码的可维护性SharedPreferences 框架体系分为以下三部分
1SharedPreferences 工具类 封装原始的 SharedPreferences 操作存、取 简化掉【mode】和【defValue】
2SPStore 定义好要使用的 【fileName】和【key】这些不让外部随意指定 将每个【key】对应的存取操作其封装成 get 和 set 方法 简化掉【fileName】
3MyApplication CommonStore 扩展 SPStore 中的 get 和 set 方法减少对 SharedPreferences 文件的直接操作、更灵活的定义默认值同时传入 context 简化掉【context】 二、SharedPreferences 框架体系具体实现
1、SharedPreferences 工具类
MySPTool.java
/*** SharedPreferences 工具类*/
public class MySPTool {/*** 存 String 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* param value 键值*/public static void setString(Context context, String fileName, String key, String value) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.putString(key, value);edit.commit();}/*** 取 String 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* return*/public static String getString(Context context, String fileName, String key) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);return sharedPreferences.getString(key, );}// ----------------------------------------------------------------------------------------------------/*** 存 int 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* param value 键值*/public static void setInt(Context context, String fileName, String key, int value) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.putInt(key, value);edit.commit();}/*** 取 int 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* return*/public static int getInt(Context context, String fileName, String key) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);return sharedPreferences.getInt(key, -1);}// ----------------------------------------------------------------------------------------------------/*** 存 float 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* param value 键值*/public static void setFloat(Context context, String fileName, String key, float value) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.putFloat(key, value);edit.commit();}/*** 取 float 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* return*/public static float getFloat(Context context, String fileName, String key) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);return sharedPreferences.getFloat(key, -1);}// ----------------------------------------------------------------------------------------------------/*** 存 boolean 类型的数据** param context 上下文对象* param fileName 文件名* param key 键名* param value 键值*/public static void setBoolean(Context context, String fileName, String key, boolean value) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.putBoolean(key, value);edit.commit();}/*** 取 boolean 类型的数据** param context 上下文对象* param fileName 文件名* param key 键值* return*/public static boolean getBoolean(Context context, String fileName, String key) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);return sharedPreferences.getBoolean(key, false);}// ----------------------------------------------------------------------------------------------------/*** 删除数据** param context 上下文对象* param fileName 文件名* param key 键名*/public static void remove(Context context, String fileName, String key) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.remove(key);edit.commit();}/*** 删除所有数据** param fileName 文件名* param context 上下文对象*/public static void clear(Context context, String fileName) {SharedPreferences sharedPreferences context.getSharedPreferences(fileName, Context.MODE_PRIVATE);SharedPreferences.Editor edit sharedPreferences.edit();edit.clear();edit.commit();}
}2、SPStore
SPStore.java
public class SPStore {private static final String SP_NAME test;// ----------------------------------------------------------------------------------------------------private static final String NAME_KEY name;private static final String AGE_KEY age;// public static String getName(Context context) {return MySPTool.getString(context, SP_NAME, NAME_KEY);}public static void setName(Context context, String name) {MySPTool.setString(context, SP_NAME, NAME_KEY, name);}public static int getAge(Context context) {return MySPTool.getInt(context, SP_NAME, AGE_KEY);}public static void setAge(Context context, int age) {MySPTool.setInt(context, SP_NAME, AGE_KEY, age);}
}3、MyApplication CommonStore
MyApplication.java
public class MyApplication extends Application {public static final String TAG MyApplication.class.getSimpleName();private static Context context;Overridepublic void onCreate() {super.onCreate();context this;}public static Context getContext() {return context;}
}CommonStore.java
public class CommonStore {private static String name;private static Integer age;// public static String getName() {if (name null) {String spName SPStore.getName(MyApplication.getContext());name spName;}return name;}public static void setName(String inputName) {SPStore.setName(MyApplication.getContext(), inputName);name inputName;}public static Integer getAge() {if (age null) {int spAge SPStore.getAge(MyApplication.getContext());if (spAge -1) spAge 0;age spAge;}return age;}public static void setAge(Integer inputAge) {SPStore.setAge(MyApplication.getContext(), inputAge);age inputAge;}
}4、测试 activity_sp_test.xml
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.SpTestActivitytools:ignoreMissingConstraintsLinearLayoutandroid:idid/ll_contentandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop20dpandroid:orientationverticalapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentEditTextandroid:idid/et_nameandroid:layout_width200dpandroid:layout_heightwrap_contentandroid:inputTypetext /EditTextandroid:idid/et_ageandroid:layout_width200dpandroid:layout_heightwrap_contentandroid:inputTypenumber //LinearLayoutLinearLayoutandroid:idid/ll_btnsandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:orientationhorizontalapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/ll_contentButtonandroid:idid/btn_readandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text读取 /Buttonandroid:idid/btn_writeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text写入 //LinearLayout
/androidx.constraintlayout.widget.ConstraintLayoutSpTestActivity.java
public class SpTestActivity extends AppCompatActivity {private Button btnRead;private Button btnWrite;private EditText etName;private EditText etAge;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sp_test);btnRead findViewById(R.id.btn_read);btnWrite findViewById(R.id.btn_write);etName findViewById(R.id.et_name);etAge findViewById(R.id.et_age);btnRead.setOnClickListener(v - {String name CommonStore.getName();Integer age CommonStore.getAge();etName.setText(name);etAge.setText(String.valueOf(age));});btnWrite.setOnClickListener(v - {String inputName etName.getText().toString();if (inputName null || inputName.equals()) {Toast.makeText(this, 存入的 name 不合法, Toast.LENGTH_SHORT).show();return;}String inputAgeStr etAge.getText().toString();int inputAge -1;try {inputAge Integer.parseInt(inputAgeStr);} catch (NumberFormatException e) {e.printStackTrace();}if (inputAge 0) {Toast.makeText(this, 存入的 age 不合法, Toast.LENGTH_SHORT).show();return;}CommonStore.setName(inputName);CommonStore.setAge(inputAge);});}
}
文章转载自: http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.mpbgy.cn.gov.cn.mpbgy.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn