猿问

无法动态更改主题

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


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



慕的地6264312
浏览 91回答 3
3回答

犯罪嫌疑人X

将更改主题的代码放在 onCreate()/onStart()/onResume() 之外的新函数中例子:package ...;import ..;public class MainActivity(){&nbsp; &nbsp; protected void onCreate(){&nbsp; &nbsp; &nbsp; &nbsp; //...Your code...&nbsp; &nbsp; }&nbsp; &nbsp; protected void onStart(){&nbsp; &nbsp; &nbsp; &nbsp; //...Your code, if you have onStart()...&nbsp; &nbsp; }&nbsp; &nbsp; protected void onResume(){&nbsp; &nbsp; &nbsp; &nbsp; //...Your code, if you have onResume()...&nbsp; &nbsp; }&nbsp; &nbsp; changeTheme(int themeMode){&nbsp; &nbsp; &nbsp; &nbsp; //Add the code as I have told below&nbsp; &nbsp; }}当用户更改单选按钮的状态时调用该函数。(这很重要,因为即使您不手动更改侦听器也是第一次调用它)。要检查是否是用户更改了它,而不是 SharedPreferences 中的值,请对单选按钮使用 onClickListener 而不是 onCheckChangedListener。这样您就可以确定用户更改了值而不是 SharedPreferences 中的值。在 OnClickListener 内部,更改单选按钮的状态,以模拟单选按钮的工作,并进行 SharedPreferences 值更新。这只是您提到的问题的解决方法,如果您的应用经常使用单选按钮的状态来执行其他工作,请不要使用它,因为这些嵌套在自身中的侦听器会占用大量空间和 CPU 周期。

慕村9548890

我自己找到了答案!我不知道怎么做,但我改变了这个editor.putString("CurrentTheme","Light"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editor.apply();对此,editor.putBoolean("CurrentTheme",true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;editor.apply();

慕森王

要实现深色/夜间主题,最好使用darktheme功能。以下代码是其实现示例:&nbsp;boolean isNight = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewModel.setThemeStatus(isNight) //save the last state of theme&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //to switch between light/dark&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AppCompatDelegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.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)
随时随地看视频慕课网APP

相关分类

Java
我要回答