无法动态更改主题

时间:2019-05-25 19:33:04

标签: java android

我正在尝试动态更改应用程序的主题。但是,只有在应用程序运行时,我才能更改它。就是说,当我终止该应用程序并再次打开它时,将应用默认主题。

MainActivity.java

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);
}

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"

    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".MainActivity"  android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".file_explorer"  android:theme="@style/AppTheme" />
    <activity android:name=".settings"  android:theme="@style/AppTheme" />
</application>

将值更改为共享首选项的代码

SharedPreferences preferences=getSharedPreferences("ThemePreferences",MODE_PRIVATE);
Editor editor=preferences.edit();
 RadioGroup rg=findViewById(R.id.radgrp);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.light:
                        Log.d("MRR","Light");
                        editor.putString("CurrentTheme","Light");
                        editor.apply();
                        break;
                    case R.id.dark:
                        Log.d("MRR","Dark");
                        editor.putString("CurrentTheme","Dark");
                        editor.apply();
                }
            }
        });

3 个答案:

答案 0 :(得分:0)

将更改主题的代码放入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
    }
}

用户 更改单选按钮的状态时,调用该函数。

这很重要,因为即使您没有手动更改它,也是第一次调用该监听器)。

要检查更改的是用户而不是SharedPreferences中的值,请使用onClickListener而不是onCheckChangedListener单选按钮。这样可以确定用户已更改了该值,而不是SharedPreferences中的值。在OnClickListener内,更改单选按钮的状态,以模拟单选按钮的工作,并进行SharedPreferences值更新。这只是您所提到问题的一种解决方法,如果您的应用经常使用单选按钮的状态来完成其他工作,则不要使用它,因为嵌套在它们内部的这些侦听器将占用大量空间和CPU周期。

答案 1 :(得分:0)

要实现暗夜主题,最好使用darktheme功能。

以下代码是其实现的示例:

 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)

答案 2 :(得分:0)

我自己找到了答案! 我不知道如何,但是我改变了这个

editor.putString("CurrentTheme","Light");
                        editor.apply();

对此,

editor.putBoolean("CurrentTheme",true);
                        editor.apply();