夜间模式的简单实现
1.修改项目中styles文件 name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"
2.使用夜间模式的设置代码,代码如下(项目)。
工具包新建:
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatDelegate;
public class SharedPreferencesUtil {
private static SharedPreferences.Editor editor;
//添加ui模式
public static void addModeUI(Context context,boolean bool){
editor = context.getSharedPreferences("mode",Context.MODE_PRIVATE).edit();
editor.putBoolean("mode_ui",bool);
editor.commit();
}
/**
* 是否是夜间模式
* @param context
* @return
*/
public static int getNight(Context context){
boolean bool = context.getSharedPreferences("mode",Context.MODE_PRIVATE).getBoolean("mode_ui",false);
return bool ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
}
}
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
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);
SharedPreferencesUtil.addModeUI(activity.getBaseContext(),true);
}else{
activity.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
SharedPreferencesUtil.addModeUI(activity.getBaseContext(),false);
}
}
/**
* 显示当前的模式
* @param activity
* @param mode
*/
public static void showModeUI(AppCompatActivity activity, int mode){
activity.getDelegate().setLocalNightMode(mode);
}
}
Mactivity中
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.myuimode.utils.SharedPreferencesUtil;
import com.example.myuimode.utils.UIModeUtil;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//打开设置页面的按钮
private Button btn_set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
btn_set = findViewById(R.id.btn_set);
btn_set.setOnClickListener(this);
}
//每次执行该方法检查是否设置夜间模式
@Override
protected void onStart() {
super.onStart();
int mode = SharedPreferencesUtil.getNight(this);
UIModeUtil.showModeUI(this,mode);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this,SetActivity.class);
startActivity(intent);
}
}
SetActivity中
//切换夜间模式的选中框
private CheckBox checkBox;
private AppCompatActivity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set);
activity = this;
initView();
}
private void initView(){
checkBox = findViewById(R.id.checkbox);
int mode = SharedPreferencesUtil.getNight(this);
if(mode == AppCompatDelegate.MODE_NIGHT_YES){
checkBox.setChecked(true);
}else{
checkBox.setChecked(false);
}
//监听夜间模式的切换
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
UIModeUtil.changeModeUI(activity);
recreate();
}
});
}
效果图
网友评论