为了账号安全,请及时绑定邮箱和手机立即绑定

无法动态更改主题

无法动态更改主题

慕的地6264312 2022-12-15 16:59:07
我正在尝试动态更改应用程序的主题。但我只能在应用程序运行时更改它。意思是,当我终止应用程序并再次打开它时,将应用默认主题。MainActivity.javapublic 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);
查看完整描述

3 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

将更改主题的代码放在 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 周期。


查看完整回答
反对 回复 2022-12-15
?
慕村9548890

TA贡献1884条经验 获得超4个赞

我自己找到了答案!我不知道怎么做,但我改变了这个

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

对此,

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


查看完整回答
反对 回复 2022-12-15
?
慕森王

TA贡献1777条经验 获得超3个赞

要实现深色/夜间主题,最好使用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)


查看完整回答
反对 回复 2022-12-15
  • 3 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信