添加夜间模式主题设置功能:跟随系统/浅色/深色模式

This commit is contained in:
Developer
2026-03-22 22:43:56 +08:00
parent 893b5356a9
commit 795d065aef
4 changed files with 184 additions and 2 deletions

View File

@@ -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")

View File

@@ -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
@@ -58,6 +61,12 @@ class SecondActivity : AppCompatActivity() {
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<HeaderConfig>()
private var promptConfigs = mutableListOf<PromptConfig>()
@@ -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 = "") {

View File

@@ -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 -> "跟随系统"
}
}
}

View File

@@ -232,7 +232,83 @@
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- 3. 自定义提示词卡片 -->
<!-- 3. 主题设置卡片 -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:cardBackgroundColor="@color/surface"
app:cardCornerRadius="16dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- 卡片标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主题设置"
android:textColor="@color/text_primary"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="设置应用主题和夜间模式"
android:textColor="@color/text_hint"
android:textSize="14sp" />
<!-- 主题选择 -->
<RadioGroup
android:id="@+id/rgThemeMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/rbThemeFollowSystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@color/primary"
android:text="跟随系统"
android:textColor="@color/text_secondary"
android:textSize="16sp" />
<RadioButton
android:id="@+id/rbThemeLight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@color/primary"
android:text="浅色模式"
android:textColor="@color/text_secondary"
android:textSize="16sp" />
<RadioButton
android:id="@+id/rbThemeDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/primary"
android:text="深色模式"
android:textColor="@color/text_secondary"
android:textSize="16sp" />
</RadioGroup>
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- 4. 自定义提示词卡片 -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
@@ -324,7 +400,7 @@
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- 4. 自定义请求头折叠卡片 -->
<!-- 5. 自定义请求头折叠卡片 -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"