继续增加配置背景图的按钮,修订以前的错误。发生了新的bug

This commit is contained in:
2024-09-30 22:28:34 +08:00
parent f3ffbde4a6
commit af71db8b40
2 changed files with 42 additions and 34 deletions

View File

@@ -1,26 +1,27 @@
package com.example.flomo_ai package com.example.flomo_ai
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle
import android.graphics.Color
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.InputStream import java.io.InputStream
class SecondActivity : AppCompatActivity() { class SecondActivity : AppCompatActivity() {
private lateinit var etApiButtonName: EditText private lateinit var etApiButtonName: EditText
private lateinit var etApiName: EditText private lateinit var etApiName: EditText
@@ -39,16 +40,18 @@ class SecondActivity : AppCompatActivity() {
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(androidx.appcompat.R.style.Theme_AppCompat)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second) // 确认布局文件正确
initViews() initViews()
loadConfigs() loadConfigs()
displayConfigs() displayConfigs()
val btnGoBack: Button = findViewById(R.id.btnGoBack) val btnGoBack: Button = findViewById(R.id.btnGoBack);
btnGoBack.setOnClickListener { btnGoBack.setOnClickListener {
finish() val intent = Intent(this, MainActivity::class.java);
startActivity(intent);
finish();
} }
btnSave.setOnClickListener { btnSave.setOnClickListener {
@@ -85,14 +88,14 @@ class SecondActivity : AppCompatActivity() {
} }
private fun initViews() { private fun initViews() {
etApiButtonName = findViewById(R.id.etApiButtonName) etApiName = findViewById(R.id.etApiName) ?: throw IllegalStateException("etApiName is not found")
etApiName = findViewById(R.id.etApiName) etApiUrl = findViewById(R.id.etApiUrl) ?: throw IllegalStateException("etApiUrl is not found")
etApiUrl = findViewById(R.id.etApiUrl) etApiKey = findViewById(R.id.etApiKey) ?: throw IllegalStateException("etApiKey is not found")
etApiKey = findViewById(R.id.etApiKey) etApiSecretKey = findViewById(R.id.etApiSecretKey) ?: throw IllegalStateException("etApiSecretKey is not found")
etApiSecretKey = findViewById(R.id.etApiSecretKey) etApiModel = findViewById(R.id.etApiModel) ?: throw IllegalStateException("etApiModel is not found")
etApiModel = findViewById(R.id.etApiModel) etApiButtonName = findViewById(R.id.etApiButtonName) ?: throw IllegalStateException("etApiButtonName is not found")
btnSave = findViewById(R.id.btnSave) btnSave = findViewById(R.id.btnSave) ?: throw IllegalStateException("btnSave is not found")
llConfigList = findViewById(R.id.llConfigList) llConfigList = findViewById(R.id.llConfigList) ?: throw IllegalStateException("llConfigList is not found")
} }
private fun loadConfigs() { private fun loadConfigs() {
@@ -106,8 +109,16 @@ class SecondActivity : AppCompatActivity() {
private fun saveConfigs() { private fun saveConfigs() {
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE) val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
val json = Gson().toJson(configs) val editor = sharedPrefs.edit()
sharedPrefs.edit().putString("configs", json).apply() if (editor != null) {
val gson = Gson()
val json = gson.toJson(configs)
Log.d("ConfigDebug", "Saving configs: $json")
editor.putString("configs", json)
editor.apply()
} else {
Log.e("ConfigDebug", "Failed to get SharedPreferences.Editor")
}
} }
private fun addConfig() { private fun addConfig() {

View File

@@ -31,10 +31,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:autofillHints=""
android:hint="API 按钮显示名称" android:hint="API 按钮显示名称"
android:minHeight="48dp" android:minHeight="48dp" />
tools:ignore="HardcodedText,TextFields" />
<EditText <EditText
android:id="@+id/etApiName" android:id="@+id/etApiName"
@@ -82,29 +80,28 @@
android:id="@+id/btnSave" android:id="@+id/btnSave"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/save_config" android:layout_marginTop="16dp"
android:layout_marginTop="16dp" /> android:text="@string/save_config" />
<LinearLayout <LinearLayout
android:id="@+id/llConfigList" android:id="@+id/llConfigList"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginTop="16dp"
android:layout_marginTop="16dp" /> android:orientation="vertical" />
android:background="#background_color"> <!-- 设置背景色 -->
<TextView <TextView
android:text="设置背景图片"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="设置背景图片"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<Button <Button
android:id="@+id/chooseButton" android:id="@+id/chooseButton"
android:text="选择图片"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true"/> android:layout_centerInParent="true"
android:text="选择图片" />
</LinearLayout> </LinearLayout>