增加修改状态栏的背景颜色的按钮和功能实现
This commit is contained in:
@@ -10,6 +10,10 @@ import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import androidx.core.content.ContextCompat
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
|
||||
|
||||
class SecondActivity : AppCompatActivity() {
|
||||
private lateinit var etApiName: EditText
|
||||
@@ -44,6 +48,10 @@ class SecondActivity : AppCompatActivity() {
|
||||
addConfig()
|
||||
}
|
||||
}
|
||||
|
||||
// 设置主页的状态栏的背景颜色
|
||||
setButtonListeners()
|
||||
|
||||
}
|
||||
|
||||
private fun initViews() {
|
||||
@@ -57,9 +65,11 @@ class SecondActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun loadConfigs() {
|
||||
// 获取一个名为 "APIConfigs" 的共享偏好设置
|
||||
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
||||
val json = sharedPrefs.getString("configs", null)
|
||||
if (json != null) {
|
||||
// 创建一个 TypeToken 的实例,用于表示一个包含 APIConfig 对象的列表类型
|
||||
val type = object : TypeToken<List<APIConfig>>() {}.type
|
||||
configs = Gson().fromJson(json, type)
|
||||
}
|
||||
@@ -190,6 +200,29 @@ class SecondActivity : AppCompatActivity() {
|
||||
etApiModel.text.clear()
|
||||
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