From 795d065aef84930e3c0e6d08e43d18c6f88b7c45 Mon Sep 17 00:00:00 2001 From: Developer Date: Sun, 22 Mar 2026 22:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9C=E9=97=B4=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=BB=E9=A2=98=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=9A=E8=B7=9F=E9=9A=8F=E7=B3=BB=E7=BB=9F/=E6=B5=85?= =?UTF-8?q?=E8=89=B2/=E6=B7=B1=E8=89=B2=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/flomo_ai/MainActivity.kt | 2 + .../com/example/flomo_ai/SecondActivity.kt | 49 ++++++++++++ .../example/flomo_ai/ui/theme/ThemeManager.kt | 55 +++++++++++++ app/src/main/res/layout/activity_second.xml | 80 ++++++++++++++++++- 4 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/example/flomo_ai/ui/theme/ThemeManager.kt diff --git a/app/src/main/java/com/example/flomo_ai/MainActivity.kt b/app/src/main/java/com/example/flomo_ai/MainActivity.kt index 935728c..f5e05d3 100644 --- a/app/src/main/java/com/example/flomo_ai/MainActivity.kt +++ b/app/src/main/java/com/example/flomo_ai/MainActivity.kt @@ -16,6 +16,7 @@ import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import com.example.flomo_ai.ui.theme.ThemeManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken import kotlinx.coroutines.CoroutineScope @@ -46,6 +47,7 @@ class MainActivity : AppCompatActivity() { @SuppressLint("MissingInflatedId", "CutPasteId", "SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + ThemeManager.applySavedTheme(this) Log.d("MainActivity", "onCreate: Starting MainActivity") setContentView(R.layout.activity_main) Log.d("MainActivity", "onCreate: Layout set") diff --git a/app/src/main/java/com/example/flomo_ai/SecondActivity.kt b/app/src/main/java/com/example/flomo_ai/SecondActivity.kt index 3c1394b..84dd1aa 100644 --- a/app/src/main/java/com/example/flomo_ai/SecondActivity.kt +++ b/app/src/main/java/com/example/flomo_ai/SecondActivity.kt @@ -17,8 +17,11 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import android.widget.Toast +import android.widget.RadioGroup +import android.widget.RadioButton import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import com.example.flomo_ai.ui.theme.ThemeManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken import kotlinx.coroutines.CoroutineScope @@ -57,6 +60,12 @@ class SecondActivity : AppCompatActivity() { private lateinit var layoutPromptContent: LinearLayout private lateinit var ivPromptArrow: ImageView private lateinit var layoutPromptToggle: LinearLayout + + // Theme view references + private lateinit var rgThemeMode: RadioGroup + private lateinit var rbThemeFollowSystem: RadioButton + private lateinit var rbThemeLight: RadioButton + private lateinit var rbThemeDark: RadioButton // Data storage private var headerConfigs = mutableListOf() @@ -68,6 +77,7 @@ class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + ThemeManager.applySavedTheme(this) Log.d("SecondActivity", "onCreate: Starting SecondActivity") try { setContentView(R.layout.activity_second) @@ -148,6 +158,12 @@ class SecondActivity : AppCompatActivity() { ivPromptArrow = findViewById(R.id.ivPromptArrow) layoutPromptToggle = findViewById(R.id.layoutPromptToggle) + // Theme Section + rgThemeMode = findViewById(R.id.rgThemeMode) + rbThemeFollowSystem = findViewById(R.id.rbThemeFollowSystem) + rbThemeLight = findViewById(R.id.rbThemeLight) + rbThemeDark = findViewById(R.id.rbThemeDark) + Log.d("SecondActivity", "initViews: All views found") // Setup API key toggle @@ -283,6 +299,39 @@ class SecondActivity : AppCompatActivity() { addPromptEntry(prompt.title, prompt.content) } } + + // Setup theme + setupTheme() + } + + private fun setupTheme() { + // Get saved theme mode + val themeMode = ThemeManager.getThemeMode(this) + + // Set the correct radio button + when (themeMode) { + ThemeManager.THEME_FOLLOW_SYSTEM -> rbThemeFollowSystem.isChecked = true + ThemeManager.THEME_LIGHT -> rbThemeLight.isChecked = true + ThemeManager.THEME_DARK -> rbThemeDark.isChecked = true + else -> rbThemeFollowSystem.isChecked = true + } + + // Set up radio group listener + rgThemeMode.setOnCheckedChangeListener { _, checkedId -> + val newMode = when (checkedId) { + R.id.rbThemeFollowSystem -> ThemeManager.THEME_FOLLOW_SYSTEM + R.id.rbThemeLight -> ThemeManager.THEME_LIGHT + R.id.rbThemeDark -> ThemeManager.THEME_DARK + else -> ThemeManager.THEME_FOLLOW_SYSTEM + } + + // Save and apply the new theme + ThemeManager.setThemeMode(this, newMode) + Log.d("SecondActivity", "Theme mode changed to: ${ThemeManager.getThemeModeName(newMode)}") + + // Recreate activity to apply theme changes + recreate() + } } private fun addHeaderEntry(key: String = "", value: String = "") { diff --git a/app/src/main/java/com/example/flomo_ai/ui/theme/ThemeManager.kt b/app/src/main/java/com/example/flomo_ai/ui/theme/ThemeManager.kt new file mode 100644 index 0000000..f2fb94e --- /dev/null +++ b/app/src/main/java/com/example/flomo_ai/ui/theme/ThemeManager.kt @@ -0,0 +1,55 @@ +package com.example.flomo_ai.ui.theme + +import android.content.Context +import android.content.SharedPreferences +import androidx.appcompat.app.AppCompatDelegate + +object ThemeManager { + private const val PREFS_NAME = "theme_prefs" + private const val KEY_THEME_MODE = "theme_mode" + + const val THEME_FOLLOW_SYSTEM = 0 + const val THEME_LIGHT = 1 + const val THEME_DARK = 2 + + private fun getPreferences(context: Context): SharedPreferences { + return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + } + + fun getThemeMode(context: Context): Int { + return getPreferences(context).getInt(KEY_THEME_MODE, THEME_FOLLOW_SYSTEM) + } + + fun setThemeMode(context: Context, mode: Int) { + getPreferences(context).edit().putInt(KEY_THEME_MODE, mode).apply() + applyTheme(mode) + } + + fun applyTheme(mode: Int) { + when (mode) { + THEME_FOLLOW_SYSTEM -> { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + } + THEME_LIGHT -> { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) + } + THEME_DARK -> { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) + } + } + } + + fun applySavedTheme(context: Context) { + val mode = getThemeMode(context) + applyTheme(mode) + } + + fun getThemeModeName(mode: Int): String { + return when (mode) { + THEME_FOLLOW_SYSTEM -> "跟随系统" + THEME_LIGHT -> "浅色模式" + THEME_DARK -> "深色模式" + else -> "跟随系统" + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_second.xml b/app/src/main/res/layout/activity_second.xml index 4a09a6e..a55c37b 100644 --- a/app/src/main/res/layout/activity_second.xml +++ b/app/src/main/res/layout/activity_second.xml @@ -232,7 +232,83 @@ - + + + + + + + + + + + + + + + + + + + + + + + + - +