Java 无法动态更改主题
我正在尝试动态更改应用程序的主题。但我只能在应用程序处于活动状态时更改它。意味着,当我关闭应用程序并再次打开它时,将应用默认主题 MainActivity.javaJava 无法动态更改主题,java,android,Java,Android,我正在尝试动态更改应用程序的主题。但我只能在应用程序处于活动状态时更改它。意味着,当我关闭应用程序并再次打开它时,将应用默认主题 MainActivity.java public class MainActivity extends AppCompatActivity { public ArrayList<FTPFile> listfile=null; Intent men; @Override protected vo
public class MainActivity extends AppCompatActivity {
public ArrayList<FTPFile> listfile=null;
Intent men;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences=getSharedPreferences("ThemePreferences",MODE_PRIVATE);
String choice=preferences.getString("CurrentTheme",null);
Toast.makeText(getApplicationContext(),"Its"+ choice,Toast.LENGTH_SHORT).show();
if(choice=="Light")
this.setTheme(R.style.AppThemeLight);
else
this.setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
将更改主题的代码放在onCreate()/onStart()/onResume()之外的新函数中 例如:
package ...;
import ..;
public class MainActivity(){
protected void onCreate(){
//...Your code...
}
protected void onStart(){
//...Your code, if you have onStart()...
}
protected void onResume(){
//...Your code, if you have onResume()...
}
changeTheme(int themeMode){
//Add the code as I have told below
}
}
当用户更改单选按钮的状态时调用该函数
(这一点很重要,因为即使您没有手动更改侦听器,它也是第一次被调用的
要检查是否是用户更改了它,而不是SharedReferences中的值,请对单选按钮使用onClickListener,而不是onCheckChangedListener。这样,您就可以确定用户更改了值,而不是SharedReferences中的值。在OnClickListener中,更改单选按钮的状态,以模拟单选按钮的工作,并执行SharedReferences值更新。这只是您提到的问题的一种解决方法,如果您的应用程序经常使用单选按钮的状态来做其他工作,请不要使用它,因为这些嵌套在其内部的侦听器将占用大量空间和CPU周期。将更改主题的代码放在onCreate()/onStart()/onResume()之外的新函数中 例如:
package ...;
import ..;
public class MainActivity(){
protected void onCreate(){
//...Your code...
}
protected void onStart(){
//...Your code, if you have onStart()...
}
protected void onResume(){
//...Your code, if you have onResume()...
}
changeTheme(int themeMode){
//Add the code as I have told below
}
}
当用户更改单选按钮的状态时调用该函数
(这一点很重要,因为即使您没有手动更改侦听器,它也是第一次被调用的
要检查是否是用户更改了它,而不是SharedReferences中的值,请对单选按钮使用onClickListener,而不是onCheckChangedListener。这样,您就可以确定用户更改了值,而不是SharedReferences中的值。在OnClickListener中,更改单选按钮的状态,以模拟单选按钮的工作,并执行SharedReferences值更新。这只是您提到的问题的一个解决方法,如果您的应用程序经常使用单选按钮的状态来做其他工作,请不要使用它,因为这些嵌套在其内部的侦听器将占用大量空间和CPU周期。要实现暗/夜主题,最好使用该功能 以下代码是其实现的示例:
boolean isNight = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES
viewModel.setThemeStatus(isNight) //save the last state of theme
//to switch between light/dark
AppCompatDelegate
.setDefaultNightMode(isNight ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES)
在您的启动器活动中:
boolean isNight = viewModel.getThemeStatus()
AppCompatDelegate.setDefaultNightMode(isNight ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES)
要实现黑暗/黑夜主题,最好使用该功能 以下代码是其实现的示例:
boolean isNight = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES
viewModel.setThemeStatus(isNight) //save the last state of theme
//to switch between light/dark
AppCompatDelegate
.setDefaultNightMode(isNight ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES)
在您的启动器活动中:
boolean isNight = viewModel.getThemeStatus()
AppCompatDelegate.setDefaultNightMode(isNight ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES)
我自己找到了答案! 我不知道怎么改的
editor.putString("CurrentTheme","Light");
editor.apply();
对此,
editor.putBoolean("CurrentTheme",true);
editor.apply();
我自己找到了答案! 我不知道怎么改的
editor.putString("CurrentTheme","Light");
editor.apply();
对此,
editor.putBoolean("CurrentTheme",true);
editor.apply();
嘿,请你展示一下你用来设置主题首选项的代码。好的!我已经添加了它。请看您需要在设置主题后调用
重新创建
,以便根据新主题重建活动。嘿,请显示您用于设置主题首选项的代码。当然!我已经添加了。请看您需要在设置主题后调用重新创建
,以便根据新主题重建活动。