簡體   English   中英

如何從我的導航開關按鈕中保存主題並將其應用於其他活動?

[英]How can I save the Theme from my navigation switch button and apply it to other activities?

如何從我的導航開關按鈕中保存主題並將其應用於其他活動? 我搜索了所有內容,但找不到任何東西

如果有任何辦法可以幫助我解決這個問題,我將不勝感激。

這是我的UserActivity class 和我的導航抽屜

public class UserActivity extends AppCompatActivity{

private AppBarConfiguration mAppBarConfiguration;

private SwitchCompat  drawerSwitch;

int NightMode;

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
        setTheme(R.style.darkTheme);
    } else {
        setTheme(R.style.AppTheme);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_switch,R.id.switchtheme)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    drawerSwitch =  (SwitchCompat) navigationView.getMenu().findItem(R.id.nav_switch).getActionView().findViewById(R.id.switchtheme);


    sharedPreferences = getSharedPreferences("SharedPrefs", MODE_PRIVATE);
    NightMode = sharedPreferences.getInt("NightModeInt", 1);

    AppCompatDelegate.setDefaultNightMode(NightMode);


    boolean value = true;

    sharedPreferences = getSharedPreferences("isChecked", MODE_PRIVATE);

    value = sharedPreferences.getBoolean("isChecked", value);
    drawerSwitch.setChecked(value);

    drawerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (drawerSwitch.isChecked()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                sharedPreferences.edit().putBoolean("isChecked", true).apply();

            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                sharedPreferences.edit().putBoolean("isChecked", false).apply();
            }
        }
    });

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.


    getMenuInflater().inflate(R.menu.user, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    NightMode = AppCompatDelegate.getDefaultNightMode();

    sharedPreferences = getSharedPreferences("SharedPrefs", MODE_PRIVATE);
    editor = sharedPreferences.edit();

    editor.putInt("NightModeInt", NightMode);
    editor.apply();
}

}

嘗試使用 SharedPreferences

val sharedPref = this.getSharedPreferences(
    "shared name", Context.MODE_PRIVATE)

sharedPref.edit().putInt("theme", AppCompatDelegate.getDefaultNightMode()).apply()
sharedPref.getInt("theme", -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM