继续增加配置背景图的按钮,修订以前的错误。发生了新的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
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.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
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.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.InputStream
class SecondActivity : AppCompatActivity() {
private lateinit var etApiButtonName: EditText
private lateinit var etApiName: EditText
@@ -39,16 +40,18 @@ class SecondActivity : AppCompatActivity() {
}
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(androidx.appcompat.R.style.Theme_AppCompat)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second) // 确认布局文件正确
initViews()
loadConfigs()
displayConfigs()
val btnGoBack: Button = findViewById(R.id.btnGoBack)
val btnGoBack: Button = findViewById(R.id.btnGoBack);
btnGoBack.setOnClickListener {
finish()
val intent = Intent(this, MainActivity::class.java);
startActivity(intent);
finish();
}
btnSave.setOnClickListener {
@@ -85,14 +88,14 @@ class SecondActivity : AppCompatActivity() {
}
private fun initViews() {
etApiButtonName = findViewById(R.id.etApiButtonName)
etApiName = findViewById(R.id.etApiName)
etApiUrl = findViewById(R.id.etApiUrl)
etApiKey = findViewById(R.id.etApiKey)
etApiSecretKey = findViewById(R.id.etApiSecretKey)
etApiModel = findViewById(R.id.etApiModel)
btnSave = findViewById(R.id.btnSave)
llConfigList = findViewById(R.id.llConfigList)
etApiName = findViewById(R.id.etApiName) ?: throw IllegalStateException("etApiName is not found")
etApiUrl = findViewById(R.id.etApiUrl) ?: throw IllegalStateException("etApiUrl is not found")
etApiKey = findViewById(R.id.etApiKey) ?: throw IllegalStateException("etApiKey is not found")
etApiSecretKey = findViewById(R.id.etApiSecretKey) ?: throw IllegalStateException("etApiSecretKey is not found")
etApiModel = findViewById(R.id.etApiModel) ?: throw IllegalStateException("etApiModel is not found")
etApiButtonName = findViewById(R.id.etApiButtonName) ?: throw IllegalStateException("etApiButtonName is not found")
btnSave = findViewById(R.id.btnSave) ?: throw IllegalStateException("btnSave is not found")
llConfigList = findViewById(R.id.llConfigList) ?: throw IllegalStateException("llConfigList is not found")
}
private fun loadConfigs() {
@@ -106,8 +109,16 @@ class SecondActivity : AppCompatActivity() {
private fun saveConfigs() {
val sharedPrefs = getSharedPreferences("APIConfigs", MODE_PRIVATE)
val json = Gson().toJson(configs)
sharedPrefs.edit().putString("configs", json).apply()
val editor = sharedPrefs.edit()
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() {

View File

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