设置两个独立imageview的图像

vlurs2pr  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(272)

我的应用程序中有两个imageview和两个按钮。每个按钮都将使用图库选取器更改其中一个图像视图。
但是我不知道如何分别为每个图像视图选择图像。
用户界面图片

lateinit var frontImageView: ImageView
    lateinit var backImageView: ImageView

    private val pickImage = 100
    private var imageUri: Uri? = null
...
   override fun onCreate(savedInstanceState: Bundle?) {
        frontImageView = findViewById(R.id.cb_image_frontside)
        backImageView = findViewById(R.id.cb_image_backside)

}

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == pickImage) {
            imageUri = data?.data

            frontImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
                                                 // But I can only always have one of the 
                                                 // imageViews changed at one time 

                                                 // How can I change the Image of "backImageView" with 
                                                 // the other button?
        }
    }
// This is the fuction for the buttons 
    fun pickMedia(view: View) {
        val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
        startActivityForResult(gallery, pickImage)
    }
tktrz96b

tktrz96b1#

根据以上代码,您可以尝试以下操作:

lateinit var frontImageView: ImageView
    lateinit var backImageView: ImageView

    lateinit var frontButton: AppCompatButton
    lateinit var backButton: AppCompatButton

    private val pickFrontImage = 100
    private val pickBackImage = 101
    private var imageUri: Uri? = null
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        frontImageView = findViewById(R.id.cb_image_frontside)
        backImageView = findViewById(R.id.cb_image_backside)

        frontButton = findViewById(R.id.frontButton)
        backButton = findViewById(R.id.backButton)

        settButtonListeners()

    }

    private fun settButtonListeners() {
        frontButton.setOnClickListener {
           pickMedia(pickFrontImage)
        }

        backButton.setOnClickListener {
            pickMedia(pickBackImage)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            imageUri = data?.data

            if (requestCode == pickFrontImage) {
                frontImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
                // But I can only always have one of the
                // imageViews changed at one time
            } else if (requestCode == pickBackImage) {
                backImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
                // But I can only always have one of the
                // imageViews changed at one time
            }
        }
    }

    // This is the fuction for the buttons
    fun pickMedia(requestCode: Int) {
        val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
        startActivityForResult(gallery, requestCode)
    }

pickMedia 函数传递requestedcode作为参数。

相关问题