完成首页的状态栏的背景颜色修改,修改按钮在配置页面。
This commit is contained in:
@@ -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