首页 > 其他 > 详细

不同activity间通过保存到SharedPreferences中实现保存状态

时间:2015-12-23 12:24:49      阅读:236      评论:0      收藏:0      [点我收藏+]

//自定义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);
}

不同activity间通过保存到SharedPreferences中实现保存状态

原文:http://www.cnblogs.com/achen0502/p/5069323.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!