完成首页的状态栏的背景颜色修改,修改按钮在配置页面。
This commit is contained in:
@@ -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? {
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.google.gson.reflect.TypeToken
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
|
||||
|
||||
class SecondActivity : AppCompatActivity() {
|
||||
@@ -27,6 +28,55 @@ class SecondActivity : AppCompatActivity() {
|
||||
private var configs = mutableListOf<APIConfig>()
|
||||
private var editingId: Long? = null
|
||||
|
||||
private fun setButtonListeners() {
|
||||
val buttonIds = listOf(
|
||||
R.id.button_holo_red_light,
|
||||
R.id.button_holo_green_light,
|
||||
R.id.button_holo_blue_light,
|
||||
R.id.button_holo_orange_light
|
||||
)
|
||||
|
||||
buttonIds.forEach { buttonId ->
|
||||
findViewById<Button>(buttonId).setOnClickListener {
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
|
||||
val editor = sharedPrefs.edit()
|
||||
// 获取颜色资源 ID
|
||||
val colorResId = getColorForButtonId(buttonId)
|
||||
// 使用正则表达式从资源名称中提取颜色值并存储
|
||||
val colorValue = extractColorValue(buttonId)
|
||||
editor.putString("buttonColor", colorValue)
|
||||
editor.apply()
|
||||
Log.d("SharedPrefsDebug", "Stored color value in SecondActivity: $colorValue")
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun getColorForButtonId(buttonId: Int): Int {
|
||||
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
||||
// API 23 及以上使用 Context.getColor()
|
||||
baseContext.getColor(findColorResourceId(buttonId))
|
||||
} else {
|
||||
// 低版本使用 ResourcesCompat.getColor()
|
||||
ResourcesCompat.getColor(resources, findColorResourceId(buttonId), null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun findColorResourceId(buttonId: Int): Int {
|
||||
return when (buttonId) {
|
||||
R.id.button_holo_red_light -> android.R.color.holo_red_light
|
||||
R.id.button_holo_green_light -> android.R.color.holo_green_light
|
||||
R.id.button_holo_blue_light -> android.R.color.holo_blue_light
|
||||
R.id.button_holo_orange_light -> android.R.color.holo_orange_light
|
||||
else -> -1
|
||||
}
|
||||
}
|
||||
|
||||
private fun extractColorValue(buttonId: Int): String {
|
||||
val resourceName = resources.getResourceEntryName(buttonId)
|
||||
val regex = Regex(".*_(.*)_light")
|
||||
val matchResult = regex.find(resourceName)
|
||||
return matchResult?.groupValues?.get(1)?: ""
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
setTheme(androidx.appcompat.R.style.Theme_AppCompat)
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -48,8 +98,7 @@ class SecondActivity : AppCompatActivity() {
|
||||
addConfig()
|
||||
}
|
||||
}
|
||||
|
||||
// 设置主页的状态栏的背景颜色
|
||||
//颜色按钮的监听
|
||||
setButtonListeners()
|
||||
|
||||
}
|
||||
@@ -201,28 +250,6 @@ class SecondActivity : AppCompatActivity() {
|
||||
btnSave.text = "保存配置"
|
||||
}
|
||||
|
||||
private fun setButtonListeners() {
|
||||
val buttonIds = listOf(
|
||||
R.id.button_holo_red_light,
|
||||
R.id.button_holo_green_light,
|
||||
R.id.button_holo_blue_light,
|
||||
R.id.button_holo_orange_light
|
||||
)
|
||||
|
||||
buttonIds.forEach { buttonId ->
|
||||
findViewById<Button>(buttonId).setOnClickListener {
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", Context.MODE_PRIVATE)
|
||||
val editor = sharedPrefs.edit()
|
||||
// 获取颜色资源 ID
|
||||
val colorResId = ContextCompat.getColor(this, buttonId)
|
||||
editor.putInt("buttonColor", colorResId)
|
||||
editor.apply()
|
||||
// 获取颜色资源名称,通过资源 ID 查找资源名称
|
||||
val res = resources.getResourceEntryName(buttonId)
|
||||
Log.d("SharedPrefsDebug", "Saved color resource name: $res")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class APIConfig(
|
||||
|
||||
Reference in New Issue
Block a user