zoukankan      html  css  js  c++  java
  • 不同activity间通过保存到SharedPreferences中实现保存状态

    //自定义SharedPreferencesUtil类

    public class SharedPreferencesUtil {

    public static String CONFIG = "config";
    private static SharedPreferences sharedPreferences;

    public static void saveStringData(Context context,String key,String value){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    sharedPreferences.edit().putString(key, value).apply();

    }

    public static String getStringData(Context context,String key,String defValue){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    return sharedPreferences.getString(key, defValue);
    }


    public static void saveIntData(Context context,String key,int value){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    sharedPreferences.edit().putInt(key, value).apply();

    }

    public static int getIntData(Context context,String key,int defValue){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    return sharedPreferences.getInt(key, defValue);
    }

    public static void saveBoolData(Context context,String key,Boolean value){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    sharedPreferences.edit().putBoolean(key, value).apply();
    }

    public static Boolean getBoolData(Context context,String key,Boolean defValue){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    return sharedPreferences.getBoolean(key, defValue);
    }

    public static void removeByKey(Context context,String key){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    sharedPreferences.edit().remove(key).apply();
    }

    public static void removeAll(Context context){
    if(sharedPreferences==null){
    sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }
    sharedPreferences.edit().clear().apply();
    }


    }

    //activity中代码的实现

    //处方不显示价格
    cb_isshow_medicine_price.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
    //处方不显示价格
    // Toast.makeText(getActivity(), "不显示", 0).show();
    SharedPreferencesUtil.saveBoolData(getActivity(), "Show", false);
    }else{
    //处方显示价格
    // Toast.makeText(getActivity(), "显示", 0).show();
    SharedPreferencesUtil.saveBoolData(getActivity(), "Show", true);
    }
    }
    });
    boolean boolData = SharedPreferencesUtil.getBoolData(getActivity(), "Show", Show);
    if(boolData){
    cb_isshow_medicine_price.setChecked(Show);
    }else{
    cb_isshow_medicine_price.setChecked(!Show);
    }

  • 相关阅读:
    尚硅谷前端2020Web前端学习记录
    阿里网盘阿里云网盘内测资格获取,阿里网盘开通
    冰眼冷链物流监控平台-2020微服务项目实战
    探花交友智能推荐社交项目-2020Java大数据实战
    互联网Java工程师面试突击三季
    恋上数据结构与算法(一、二、三季)
    布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.9
    NumPy 基础知识·翻译完成
    NumPy 初学者指南中文第三版·翻译完成
    NumPy 秘籍中文第二版·翻译完成
  • 原文地址:https://www.cnblogs.com/achen0502/p/5069323.html
Copyright © 2011-2022 走看看