夜间模式
2020-06-25 本文已影响0人
夜沐下的星雨
1.切换夜间模式的工具类
注意:其中MODE要自己定义一个常量,mMode要在application中创建
/*
* 使用这个类需要把style设置成日夜间的样式:Theme.AppCompat.DayNight.NoActionBar
*/
public class UIModeUtil {
/**
* 夜间模式切换
*/
public static void changeModeUI(AppCompatActivity activity){
int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_NO){
//日间模式,切成夜间模式
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
SpUtil.setParam(Constants.MODE,AppCompatDelegate.MODE_NIGHT_YES);
BaseApp.mMode = AppCompatDelegate.MODE_NIGHT_YES;
}else{
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
SpUtil.setParam(Constants.MODE,AppCompatDelegate.MODE_NIGHT_NO);
BaseApp.mMode = AppCompatDelegate.MODE_NIGHT_NO;
}
}
/**
* 设置当前的模式
* @param activity
*/
public static void setActModeFromSp(AppCompatActivity activity){
int mode = (int) SpUtil.getParam(Constants.MODE, AppCompatDelegate.MODE_NIGHT_NO);
activity.getDelegate().setLocalNightMode(mode);
}
/**
* 设置当前的模式
* @param activity
*/
public static void setActModeUseMode(AppCompatActivity activity,int mode){
activity.getDelegate().setLocalNightMode(mode);
}
public static void setAppMode(int mode){
AppCompatDelegate.setDefaultNightMode(mode);
}
}
2.SP工具类:
public class SpUtil {
/**
* 保存在手机里面的文件名
*/
private static final String FILE_NAME = "share_date";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
* @param key
* @param object
*/
public static void setParam( String key, Object object){
String type = object.getClass().getSimpleName();
SharedPreferences sp = BaseApp.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if("String".equals(type)){
editor.putString(key, (String)object);
}
else if("Integer".equals(type)){
editor.putInt(key, (Integer)object);
}
else if("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)object);
}
else if("Float".equals(type)){
editor.putFloat(key, (Float)object);
}
else if("Long".equals(type)){
editor.putLong(key, (Long)object);
}
editor.commit();
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
* @param key
* @param defaultObject
* @return
*/
public static Object getParam(String key, Object defaultObject){
String type = defaultObject.getClass().getSimpleName();
SharedPreferences sp = BaseApp.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
if("String".equals(type)){
return sp.getString(key, (String)defaultObject);
}
else if("Integer".equals(type)){
return sp.getInt(key, (Integer)defaultObject);
}
else if("Boolean".equals(type)){
return sp.getBoolean(key, (Boolean)defaultObject);
}
else if("Float".equals(type)){
return sp.getFloat(key, (Float)defaultObject);
}
else if("Long".equals(type)){
return sp.getLong(key, (Long)defaultObject);
}
return null;
}
}
3.在fragment中实现:
@Override
protected void initView() {
super.initView();
if (BaseApplication.mMode == AppCompatDelegate.MODE_NIGHT_NO) {
cbNight.setChecked(false);
} else {
cbNight.setChecked(true);
}
}
@Override
protected void initListener() {
super.initListener();
cbNight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//判断是否是用户操作
if (compoundButton.isPressed()) {
UIModeUtil.changeModeUI((MainActivity) getActivity());
//保存当前fragment的位置
SharedPreferencesUtil.setParam(mContext, Constants.FRAGMENT_POSITION, FragmentType.TYPE_SETTINGS);
}
}
});
}
4.在application中初始化:
public class BaseApp extends Application{
private static BaseApp baseApp;
public static int mMode= AppCompatDelegate.MODE_NIGHT_NO;
@Override
public void onCreate() {
super.onCreate();
baseApp=this;
dayOrNight();
}
private void dayOrNight() {
mMode = (int) SpUtil.getParam(Constants.MODE, AppCompatDelegate.MODE_NIGHT_NO);
UIModeUtil.setAppMode(mMode);
}
public static BaseApp getInstance(){
return baseApp;
}
}
5.在activity中默认碎片是根据保存的fragment的位置:
private void addZhiFragment() {
int pos = (int) SpUtil.getParam(Constants.NIGHT_CURRENT_FRAG_POS, 1);
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame, list.get(0));
transaction.commit();
}
6.应用退出后保存主页面:
@Override
public boolean onTouchEvent(MotionEvent event) {
SpUtil.setParam(Constants.NIGHT_CURRENT_FRAG_POS, MainActivity.TYPE_ZHIHU);
return super.onTouchEvent(event);
}
7.将主题更跟改成切换模式
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>