0

我在我的应用程序的设置页面中创建了一个功能,其中包含一个开关 - 按下时 - 切换到辅助“夜间”主题。我大部分时间都按照本教程进行操作。但是,我不知道如何将这种夜间模式带入我的其他活动中?我试过在我的主要活动中调用“如果开关检查”,但它显然没有看到那个开关。我主要需要知道的是,如何检查另一个活动中的开关状态?这甚至是正确的做法吗?让我知道我是否在这个问题上遗漏了任何其他内容。

// ======== 设置页面的代码 ======== //

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);


    // ======== Night Mode ======== //
    SwitchCompat switchCompat;
    final SharedPref sharedPref;

    sharedPref = new SharedPref(this);

    if (sharedPref.loadNightModeState()) {
        setTheme(R.style.AppTheme_Night);
        getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
    } else setTheme(R.style.AppTheme);

    setContentView(R.layout.activity_settings);
    switchCompat = (SwitchCompat) findViewById(R.id.night_switch);
    if (sharedPref.loadNightModeState()) {
        switchCompat.setChecked(true);
    }

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
               sharedPref.setNightModeState(true);
                restartApp();
            } else {
                sharedPref.setNightModeState(false);
                restartApp();
            }
        }
    });
}




private void restartApp() {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();
}

// ======== SharedPref 代码 ======== //

public class SharedPref {

private SharedPreferences sharedPreferences;


public SharedPref(Context context) {
    sharedPreferences = context.getSharedPreferences("filename", Context.MODE_PRIVATE);
}


public void setNightModeState(Boolean state) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("NightMode",state);
    editor.apply();
}

public Boolean loadNightModeState (){
    Boolean state = sharedPreferences.getBoolean("NightMode", false);
    return state;
}
4

2 回答 2

1

在你的应用程序类里面onCreate

SharedPreferences sharedPreferences = getSharedPreferences("Your_Shared_pref", Context.MODE_PRIVATE);
boolean nightMode = sharedPreferences.getBoolean(SettingsActivity.DARK_THEME_PREFERENCE_KEY, false);

AppCompatDelegate.setDefaultNightMode(nightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO);

比在您的活动中,请执行以下操作:

 switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        AppCompatDelegate.setDefaultNightMode(isChecked ? MODE_NIGHT_YES : MODE_NIGHT_NO);
        if (isChecked) {
           sharedPref.setNightModeState(true);
            recreate();
        } else {
            sharedPref.setNightModeState(false);
            recreate();
        }
    }
});

@Override
public void recreate() {
    finish();
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
    startActivity(getIntent());
    overridePendingTransition(R.anim.anime_fade_in,
            R.anim.anime_fade_out);
}

你可以在网上找到动画xml。

于 2019-01-17T16:54:18.533 回答
0

我把它放在我的 onCreate 和我的 super.onCreate 之间。我使用的代码取自我的设置 java 页面。事实证明这很容易理解,我只需要有人把它放在眼里!

 @Override
    protected void onCreate(Bundle savedInstanceState) {


        final SharedPref sharedPref;
        sharedPref = new SharedPref(this);

        if (sharedPref.loadNightModeState()) {
            setTheme(R.style.AppTheme_Night);
            //restartApp();
            //getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
            //actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
        } else setTheme(R.style.AppTheme);
        //restartApp();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
于 2019-01-18T11:27:55.987 回答