增加修改状态栏的背景颜色的按钮和功能实现
This commit is contained in:
@@ -35,9 +35,7 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
|||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.graphics.drawable.BitmapDrawable
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.widget.LinearLayout
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
// 定义请求体数据类
|
// 定义请求体数据类
|
||||||
@@ -333,13 +331,14 @@ class MainActivity : AppCompatActivity() {
|
|||||||
tabLayout = findViewById(R.id.tabLayout)
|
tabLayout = findViewById(R.id.tabLayout)
|
||||||
submitToServerButton = findViewById(R.id.submitToServerButton)
|
submitToServerButton = findViewById(R.id.submitToServerButton)
|
||||||
|
|
||||||
|
// 点击配置按钮
|
||||||
configButton = findViewById(R.id.configButton)
|
configButton = findViewById(R.id.configButton)
|
||||||
configButton.setOnClickListener {
|
configButton.setOnClickListener {
|
||||||
val intent = Intent(this, SecondActivity::class.java)
|
val intent = Intent(this, SecondActivity::class.java)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 提交到flomo的服务器按钮
|
||||||
submitToServerButton = findViewById(R.id.submitToServerButton)
|
submitToServerButton = findViewById(R.id.submitToServerButton)
|
||||||
inputEditText = findViewById(R.id.inputEditText)
|
inputEditText = findViewById(R.id.inputEditText)
|
||||||
|
|
||||||
@@ -349,7 +348,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Setup TabLayout using a loop
|
// 创建4个按钮
|
||||||
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
|
val tabLayout = findViewById<TabLayout>(R.id.tabLayout)
|
||||||
// 维持原来的创建标签按钮的代码
|
// 维持原来的创建标签按钮的代码
|
||||||
(1..4).forEach { tabIndex ->
|
(1..4).forEach { tabIndex ->
|
||||||
@@ -359,17 +358,22 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建背景图
|
}
|
||||||
val mainLinearLayout: LinearLayout = findViewById(R.id.mainLinearLayout)
|
|
||||||
|
|
||||||
// 从Intent中获取Uri
|
override fun onStart() {
|
||||||
val selectedImageUri: Uri? = intent.data
|
super.onStart()
|
||||||
selectedImageUri?.let { uri ->
|
// 获取从其他 Activity 传递过来的按钮颜色值,如果没有传递颜色值,则默认值为透明色。
|
||||||
val bitmap = getBitmapFromUri(uri)
|
val statusTextView = findViewById<TextView>(R.id.statusTextView)
|
||||||
bitmap?.let {
|
|
||||||
mainLinearLayout.background = BitmapDrawable(resources, it)
|
// 获取共享偏好设置实例
|
||||||
}
|
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
||||||
|
// 从共享偏好设置中读取按钮颜色值
|
||||||
|
var buttonColor = sharedPrefs.getInt("buttonColor", -1)
|
||||||
|
if (buttonColor == 0 || buttonColor == -1) {
|
||||||
|
buttonColor = android.R.color.holo_blue_dark
|
||||||
}
|
}
|
||||||
|
statusTextView.setBackgroundResource(buttonColor)
|
||||||
|
Log.d("SharedPrefsDebug", "Loaded color in MainActivity: $buttonColor")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getBitmapFromUri(uri: Uri): Bitmap? {
|
private fun getBitmapFromUri(uri: Uri): Bitmap? {
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ import android.widget.TextView
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
|
||||||
class SecondActivity : AppCompatActivity() {
|
class SecondActivity : AppCompatActivity() {
|
||||||
private lateinit var etApiName: EditText
|
private lateinit var etApiName: EditText
|
||||||
@@ -44,6 +48,10 @@ class SecondActivity : AppCompatActivity() {
|
|||||||
addConfig()
|
addConfig()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置主页的状态栏的背景颜色
|
||||||
|
setButtonListeners()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initViews() {
|
private fun initViews() {
|
||||||
@@ -57,9 +65,11 @@ class SecondActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun loadConfigs() {
|
private fun loadConfigs() {
|
||||||
|
// 获取一个名为 "APIConfigs" 的共享偏好设置
|
||||||
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
|
||||||
val json = sharedPrefs.getString("configs", null)
|
val json = sharedPrefs.getString("configs", null)
|
||||||
if (json != null) {
|
if (json != null) {
|
||||||
|
// 创建一个 TypeToken 的实例,用于表示一个包含 APIConfig 对象的列表类型
|
||||||
val type = object : TypeToken<List<APIConfig>>() {}.type
|
val type = object : TypeToken<List<APIConfig>>() {}.type
|
||||||
configs = Gson().fromJson(json, type)
|
configs = Gson().fromJson(json, type)
|
||||||
}
|
}
|
||||||
@@ -190,6 +200,29 @@ class SecondActivity : AppCompatActivity() {
|
|||||||
etApiModel.text.clear()
|
etApiModel.text.clear()
|
||||||
btnSave.text = "保存配置"
|
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(
|
data class APIConfig(
|
||||||
|
|||||||
@@ -122,12 +122,12 @@
|
|||||||
android:layout_below="@id/statustextView">
|
android:layout_below="@id/statustextView">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button1"
|
android:id="@+id/button_holo_red_light"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="20dp"
|
android:layout_height="20dp"
|
||||||
android:layout_marginEnd="5dp"
|
android:layout_marginEnd="5dp"
|
||||||
android:background="@android:color/holo_red_light"
|
android:background="@android:color/holo_red_light"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/button2"
|
app:layout_constraintEnd_toStartOf="@+id/button_holo_green_light"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintHorizontal_weight="1"
|
app:layout_constraintHorizontal_weight="1"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
@@ -135,33 +135,33 @@
|
|||||||
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button2"
|
android:id="@+id/button_holo_green_light"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="20dp"
|
android:layout_height="20dp"
|
||||||
android:layout_marginEnd="5dp"
|
android:layout_marginEnd="5dp"
|
||||||
android:background="@android:color/holo_green_light"
|
android:background="@android:color/holo_green_light"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/button3"
|
app:layout_constraintEnd_toStartOf="@+id/button_holo_blue_light"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintHorizontal_weight="1"
|
app:layout_constraintHorizontal_weight="1"
|
||||||
app:layout_constraintStart_toEndOf="@+id/button1"
|
app:layout_constraintStart_toEndOf="@+id/button_holo_red_light"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button3"
|
android:id="@+id/button_holo_blue_light"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="20dp"
|
android:layout_height="20dp"
|
||||||
android:layout_marginEnd="5dp"
|
android:layout_marginEnd="5dp"
|
||||||
android:background="@android:color/holo_blue_light"
|
android:background="@android:color/holo_blue_light"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/button4"
|
app:layout_constraintEnd_toStartOf="@+id/button_holo_orange_light"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintHorizontal_weight="1"
|
app:layout_constraintHorizontal_weight="1"
|
||||||
app:layout_constraintStart_toEndOf="@+id/button2"
|
app:layout_constraintStart_toEndOf="@+id/button_holo_green_light"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button4"
|
android:id="@+id/button_holo_orange_light"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="20dp"
|
android:layout_height="20dp"
|
||||||
android:layout_marginEnd="5dp"
|
android:layout_marginEnd="5dp"
|
||||||
@@ -169,7 +169,7 @@
|
|||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintHorizontal_weight="1"
|
app:layout_constraintHorizontal_weight="1"
|
||||||
app:layout_constraintStart_toEndOf="@+id/button3"
|
app:layout_constraintStart_toEndOf="@+id/button_holo_blue_light"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user