完成首页的状态栏的背景颜色修改,修改按钮在配置页面。

This commit is contained in:
2024-10-10 21:49:15 +08:00
parent 82c7f28f0f
commit 449c2f127c
2 changed files with 83 additions and 32 deletions

View File

@@ -37,6 +37,9 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import java.io.InputStream
import android.content.Context
// 定义请求体数据类
data class ChatRequest(
@@ -364,16 +367,37 @@ class MainActivity : AppCompatActivity() {
super.onStart()
// 获取从其他 Activity 传递过来的按钮颜色值,如果没有传递颜色值,则默认值为透明色。
val statusTextView = findViewById<TextView>(R.id.statusTextView)
updateStatusTextViewColor(statusTextView)
}
// 获取共享偏好设置实例
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
// 从共享偏好设置中读取按钮颜色值
var buttonColor = sharedPrefs.getInt("buttonColor", -1)
if (buttonColor == 0 || buttonColor == -1) {
buttonColor = android.R.color.holo_blue_dark
private fun updateStatusTextViewColor(statusTextView: TextView) {
try {
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
val colorValue = sharedPrefs.getString("buttonColor", "")
Log.d("SharedPrefsDebug", "读取颜色值 MainActivity: $colorValue")
if (colorValue.isNullOrEmpty()) {
statusTextView.setBackgroundResource(android.R.color.holo_blue_dark)
} else {
// 根据提取的颜色值设置背景颜色,这里只是一个示例,实际应用中可能需要更复杂的逻辑
when (colorValue) {
"red" -> statusTextView.setBackgroundResource(android.R.color.holo_red_light)
"green" -> statusTextView.setBackgroundResource(android.R.color.holo_green_light)
"blue" -> statusTextView.setBackgroundResource(android.R.color.holo_blue_light)
"orange" -> statusTextView.setBackgroundResource(android.R.color.holo_orange_light)
else -> {
// 如果颜色值不在预定义的列表中,使用默认颜色
statusTextView.setBackgroundResource(android.R.color.holo_blue_dark)
Log.e("SharedPrefsError", "Invalid color value: $colorValue")
}
}
}
} catch (e: Exception) {
// 捕获并记录任何异常
Log.e("SharedPrefsError", "更新状态文本视图颜色时出错\n: ${e.message}", e)
// 设置默认颜色
statusTextView.setBackgroundResource(android.R.color.holo_blue_dark)
}
statusTextView.setBackgroundResource(buttonColor)
Log.d("SharedPrefsDebug", "Loaded color in MainActivity: $buttonColor")
}
private fun getBitmapFromUri(uri: Uri): Bitmap? {