第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法動態(tài)更改主題

無法動態(tài)更改主題

慕的地6264312 2022-12-15 16:59:07
我正在嘗試動態(tài)更改應用程序的主題。但我只能在應用程序運行時更改它。意思是,當我終止應用程序并再次打開它時,將應用默認主題。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條經(jīng)驗 獲得超4個贊

將更改主題的代碼放在 onCreate()/onStart()/onResume() 之外的新函數(shù)中


例子:


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

    }

}

當用戶更改單選按鈕的狀態(tài)時調(diào)用該函數(shù)。


(這很重要,因為即使您不手動更改偵聽器也是第一次調(diào)用它)。


要檢查是否是用戶更改了它,而不是 SharedPreferences 中的值,請對單選按鈕使用 onClickListener 而不是 onCheckChangedListener。這樣您就可以確定用戶更改了值而不是 SharedPreferences 中的值。在 OnClickListener 內(nèi)部,更改單選按鈕的狀態(tài),以模擬單選按鈕的工作,并進行 SharedPreferences 值更新。這只是您提到的問題的解決方法,如果您的應用經(jīng)常使用單選按鈕的狀態(tài)來執(zhí)行其他工作,請不要使用它,因為這些嵌套在自身中的偵聽器會占用大量空間和 CPU 周期。


查看完整回答
反對 回復 2022-12-15
?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超4個贊

我自己找到了答案!我不知道怎么做,但我改變了這個

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

對此,

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


查看完整回答
反對 回復 2022-12-15
?
慕森王

TA貢獻1777條經(jīng)驗 獲得超3個贊

要實現(xiàn)深色/夜間主題,最好使用darktheme功能。


以下代碼是其實現(xiàn)示例:


 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 關(guān)注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號