如何将变量控件名称传递给 Kotlin 中的 findViewById?
How can I pass a variable control name to findViewById in Kotlin?
我正在尝试学习 Kotlin,并且正在构建一个简单的示例。我有 3 个图像按钮,可以打开相机并拍照。然后将缩略图设置到 ImageView 中。我已经使用 https://developer.android.com/training/camera/photobasics?hl=en 中的示例来了解基础知识(想一想如果我能让它适用于一个人,它就会适用于所有人。它确实适用于一个人,但我不知道如何使其成为将缩略图放入正确的 ImageView 中的一项功能。
在我的 onCreate 中,我为每个将调用相机的按钮设置了侦听器:
camRead1.setOnClickListener {dispatchTakePictureIntent() }
camRead2.setOnClickListener {dispatchTakePictureIntent() }
camRead3.setOnClickListener {dispatchTakePictureIntent() }
我从上面的 url 中提取了示例:
val REQUEST_IMAGE_CAPTURE = 1
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val thumb: ImageView = findViewById(R.id.thumbRead1)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
thumb.setImageBitmap(imageBitmap)
}
}
并将其粘贴到我的 class MainActivity 中,在我用变量 (thumb) 替换覆盖函数中的 imageView 并添加 super 后,它对第一个完美运行。
但是,我正在尝试获取 3 张照片,read1、read2 和 read3,每张照片都需要在 thumbRead1、thumbRead2 和 thumbRead3 中显示拇指。我无法弄清楚 onActivityResult 是如何执行的,因为 dispatchTakePictureIntent 中的调用正在调用 startActivityForResult(尤其是 Android Studio 表示 startActivityForResult 已被弃用)。
显然,onActivityResult 执行后,我可以看到 thumb 定义了 R.id.thumbRead1 并接收 imageBitmap,但我不明白如何让它知道被单击的按钮。
在不了解如何调用 onActivityResult 的情况下,我在想如果我可以做类似的事情:
findViewById(R.id("thumbRead" + imgID))
定义我想要将照片粘贴到的特定 ImageView。我走在正确的轨道上吗?如果没有,推荐的方法是什么?
请注意,他们最近添加了一种更简洁的方式来启动其他活动以获得结果,explained here。但由于您已经以传统方式进行操作,我将解释如何使其正常工作。
我认为在这种情况下最简单的做法就是制作更多请求代码,这样您就可以检查它是哪个请求。
val REQUEST_IMAGE_CAPTURE_SOURCE_1 = 1
val REQUEST_IMAGE_CAPTURE_SOURCE_2 = 2
val REQUEST_IMAGE_CAPTURE_SOURCE_3 = 3
//...
camRead1.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_1) }
camRead2.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_2) }
camRead3.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_3) }
//...
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode != RESULT_OK) {
// possibly show message to user
return
}
val imageViewId = when (requestCode) {
REQUEST_IMAGE_CAPTURE_SOURCE_1 -> R.id.thumbRead1
REQUEST_IMAGE_CAPTURE_SOURCE_2 -> R.id.thumbRead2
REQUEST_IMAGE_CAPTURE_SOURCE_3 -> R.id.thumbRead3
}
val imageView = findViewById<ImageView>(imageViewId)
imageView.imageBitmap = data.extras.get("data") as Bitmap
}
顺便说一句,如果您想像您正在尝试的那样使用字符串获取视图的 ID,您可以这样做:
val viewId = resources.getIdentifier("thumbRead$imgId", "id", packageName)
val imageView = findViewById<ImageView>(viewId)
每次调用需要传递不同的请求码,传递给dispatchTakePictureIntent函数。您不需要通过 findviewbyid 获取 id。您只需根据请求代码添加图片即可。
val REQUEST_IMAGE_CAPTURE_ONE = 1
val REQUEST_IMAGE_CAPTURE_TWO = 2
val REQUEST_IMAGE_CAPTURE_THREE = 3
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
if (requestCode == REQUEST_IMAGE_CAPTURE_ONE ) {
thumbRead1.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_TWO ) {
thumbRead2.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_THREE ) {
thumbRead3.setImageBitmap(imageBitmap)
}
}
}
我正在尝试学习 Kotlin,并且正在构建一个简单的示例。我有 3 个图像按钮,可以打开相机并拍照。然后将缩略图设置到 ImageView 中。我已经使用 https://developer.android.com/training/camera/photobasics?hl=en 中的示例来了解基础知识(想一想如果我能让它适用于一个人,它就会适用于所有人。它确实适用于一个人,但我不知道如何使其成为将缩略图放入正确的 ImageView 中的一项功能。
在我的 onCreate 中,我为每个将调用相机的按钮设置了侦听器:
camRead1.setOnClickListener {dispatchTakePictureIntent() }
camRead2.setOnClickListener {dispatchTakePictureIntent() }
camRead3.setOnClickListener {dispatchTakePictureIntent() }
我从上面的 url 中提取了示例:
val REQUEST_IMAGE_CAPTURE = 1
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val thumb: ImageView = findViewById(R.id.thumbRead1)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
thumb.setImageBitmap(imageBitmap)
}
}
并将其粘贴到我的 class MainActivity 中,在我用变量 (thumb) 替换覆盖函数中的 imageView 并添加 super 后,它对第一个完美运行。
但是,我正在尝试获取 3 张照片,read1、read2 和 read3,每张照片都需要在 thumbRead1、thumbRead2 和 thumbRead3 中显示拇指。我无法弄清楚 onActivityResult 是如何执行的,因为 dispatchTakePictureIntent 中的调用正在调用 startActivityForResult(尤其是 Android Studio 表示 startActivityForResult 已被弃用)。
显然,onActivityResult 执行后,我可以看到 thumb 定义了 R.id.thumbRead1 并接收 imageBitmap,但我不明白如何让它知道被单击的按钮。
在不了解如何调用 onActivityResult 的情况下,我在想如果我可以做类似的事情:
findViewById(R.id("thumbRead" + imgID))
定义我想要将照片粘贴到的特定 ImageView。我走在正确的轨道上吗?如果没有,推荐的方法是什么?
请注意,他们最近添加了一种更简洁的方式来启动其他活动以获得结果,explained here。但由于您已经以传统方式进行操作,我将解释如何使其正常工作。
我认为在这种情况下最简单的做法就是制作更多请求代码,这样您就可以检查它是哪个请求。
val REQUEST_IMAGE_CAPTURE_SOURCE_1 = 1
val REQUEST_IMAGE_CAPTURE_SOURCE_2 = 2
val REQUEST_IMAGE_CAPTURE_SOURCE_3 = 3
//...
camRead1.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_1) }
camRead2.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_2) }
camRead3.setOnClickListener { dispatchTakePictureIntent(REQUEST_IMAGE_CAPTURE_SOURCE_3) }
//...
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode != RESULT_OK) {
// possibly show message to user
return
}
val imageViewId = when (requestCode) {
REQUEST_IMAGE_CAPTURE_SOURCE_1 -> R.id.thumbRead1
REQUEST_IMAGE_CAPTURE_SOURCE_2 -> R.id.thumbRead2
REQUEST_IMAGE_CAPTURE_SOURCE_3 -> R.id.thumbRead3
}
val imageView = findViewById<ImageView>(imageViewId)
imageView.imageBitmap = data.extras.get("data") as Bitmap
}
顺便说一句,如果您想像您正在尝试的那样使用字符串获取视图的 ID,您可以这样做:
val viewId = resources.getIdentifier("thumbRead$imgId", "id", packageName)
val imageView = findViewById<ImageView>(viewId)
每次调用需要传递不同的请求码,传递给dispatchTakePictureIntent函数。您不需要通过 findviewbyid 获取 id。您只需根据请求代码添加图片即可。
val REQUEST_IMAGE_CAPTURE_ONE = 1
val REQUEST_IMAGE_CAPTURE_TWO = 2
val REQUEST_IMAGE_CAPTURE_THREE = 3
private fun dispatchTakePictureIntent(requestCode: Int) {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, requestCode)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
if (requestCode == REQUEST_IMAGE_CAPTURE_ONE ) {
thumbRead1.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_TWO ) {
thumbRead2.setImageBitmap(imageBitmap)
}else if (requestCode == REQUEST_IMAGE_CAPTURE_THREE ) {
thumbRead3.setImageBitmap(imageBitmap)
}
}
}