点击增加背景图
This commit is contained in:
@@ -38,7 +38,8 @@ import android.graphics.BitmapFactory
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.app.Activity
|
||||||
|
import android.widget.ImageView
|
||||||
|
|
||||||
|
|
||||||
// 定义请求体数据类
|
// 定义请求体数据类
|
||||||
@@ -363,6 +364,17 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (requestCode == SecondActivity.REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
|
||||||
|
val selectedImageUri = data.getStringExtra("selectedImageUri")
|
||||||
|
if (selectedImageUri != null) {
|
||||||
|
val imageView = findViewById<ImageView>(R.id.imageViewBackground) // 假设你的背景是一个ImageView
|
||||||
|
imageView.setImageURI(Uri.parse(selectedImageUri))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
// 获取从其他 Activity 传递过来的按钮颜色值,如果没有传递颜色值,则默认值为透明色。
|
// 获取从其他 Activity 传递过来的按钮颜色值,如果没有传递颜色值,则默认值为透明色。
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ import androidx.core.content.ContextCompat
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.content.res.ResourcesCompat
|
import androidx.core.content.res.ResourcesCompat
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
|
||||||
|
|
||||||
class SecondActivity : AppCompatActivity() {
|
class SecondActivity : AppCompatActivity() {
|
||||||
@@ -28,6 +32,11 @@ class SecondActivity : AppCompatActivity() {
|
|||||||
private var configs = mutableListOf<APIConfig>()
|
private var configs = mutableListOf<APIConfig>()
|
||||||
private var editingId: Long? = null
|
private var editingId: Long? = null
|
||||||
|
|
||||||
|
private val pickImageLauncher = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
|
||||||
|
if (uri!= null) {
|
||||||
|
// 处理选中的图片
|
||||||
|
}
|
||||||
|
}
|
||||||
private fun setButtonListeners() {
|
private fun setButtonListeners() {
|
||||||
val buttonIds = listOf(
|
val buttonIds = listOf(
|
||||||
R.id.button_holo_red_light,
|
R.id.button_holo_red_light,
|
||||||
@@ -101,6 +110,30 @@ class SecondActivity : AppCompatActivity() {
|
|||||||
//颜色按钮的监听
|
//颜色按钮的监听
|
||||||
setButtonListeners()
|
setButtonListeners()
|
||||||
|
|
||||||
|
// 背景按钮的监听
|
||||||
|
val buttonChooseImage = findViewById<Button>(R.id.buttonChooseImage)
|
||||||
|
buttonChooseImage.setOnClickListener {
|
||||||
|
pickImageLauncher.launch(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data!= null) {
|
||||||
|
val selectedImageUri: Uri? = data.data
|
||||||
|
if (selectedImageUri!= null) {
|
||||||
|
// 将选择的图片 URI 传递回 MainActivity
|
||||||
|
val resultIntent = Intent()
|
||||||
|
resultIntent.putExtra("selectedImageUri", selectedImageUri.toString())
|
||||||
|
setResult(RESULT_OK, resultIntent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val REQUEST_CODE_PICK_IMAGE = 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initViews() {
|
private fun initViews() {
|
||||||
|
|||||||
@@ -124,4 +124,22 @@
|
|||||||
android:textColor="@android:color/white"
|
android:textColor="@android:color/white"
|
||||||
android:textSize="12sp" />
|
android:textSize="12sp" />
|
||||||
|
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="#FFFFFF"> <!-- 设置默认背景颜色 -->
|
||||||
|
|
||||||
|
|
||||||
|
<!-- ImageView 作为背景 -->
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageViewBackground"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:scaleType="centerCrop"
|
||||||
|
android:contentDescription="@string/background_image" />
|
||||||
|
|
||||||
|
<!-- 其他布局元素 -->
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -175,8 +175,29 @@
|
|||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textViewImageHint"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="点击按钮选择图片作为背景"
|
||||||
|
android:layout_below="@id/buttonChooseImage"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_marginTop="10dp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/buttonChooseImage"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="选择图片"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_marginTop="20dp" />
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
@@ -13,5 +13,6 @@
|
|||||||
<string name="set_background_image">设置背景图片</string>
|
<string name="set_background_image">设置背景图片</string>
|
||||||
<string name="choose_image">选择图片</string>
|
<string name="choose_image">选择图片</string>
|
||||||
<string name="api_key">API密钥</string>
|
<string name="api_key">API密钥</string>
|
||||||
|
<string name="background_image">Background image description</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user