android - 如何重构具有 3 个相似方法的 onButtonClick?

标签 android kotlin android-intent android-activity refactoring

下面的代码完全符合我的要求。

fun onButtonClick(@Suppress("UNUSED_PARAMETER") v: View) {

    val intent = Intent(this, ImageActivity::class.java)
    val bundle = Bundle()

    orangeButton.setOnClickListener{
        val parcel:ImageUrl = IMAGE_URL_ORANGE
        bundle.putParcelable("key", parcel)
        intent.putExtra(IMAGE_BUNDLE_NAME, bundle)
        startActivity(intent)
    }

    redButton.setOnClickListener{
        val parcel:ImageUrl = IMAGE_URL_RED
        bundle.putParcelable("key", parcel)
        intent.putExtra(IMAGE_BUNDLE_NAME, bundle)
        startActivity(intent)
    }

    greenButton.setOnClickListener{
        val parcel:ImageUrl = IMAGE_URL_GREEN
        bundle.putParcelable("key", parcel)
        intent.putExtra(IMAGE_BUNDLE_NAME, bundle)
        startActivity(intent)
    }
}

问题是三种情况下的行为几乎相同。我该如何重构它?我尝试了以下操作,但它会导致应用程序崩溃。

fun onButtonClick(@Suppress("UNUSED_PARAMETER") v: View) {

    val intent = Intent(this, ImageActivity::class.java)
    val bundle = Bundle()
    lateinit var parcel:ImageUrl

    orangeButton.setOnClickListener{
        parcel = IMAGE_URL_ORANGE
    }

    redButton.setOnClickListener{
        parcel = IMAGE_URL_RED
    }

    greenButton.setOnClickListener{
        parcel = IMAGE_URL_GREEN
    }

    bundle.putParcelable("key", parcel)
    intent.putExtra(IMAGE_BUNDLE_NAME, bundle)
    startActivity(intent)
}

我可能应该使用某种 IF 语句,但我如何找到被点击按钮的 ID?

最佳答案

这是实现它的一种方式。基本上我们使用按钮 View id 将其映射到您的 ImageUrl .当用户单击按钮时,我们检索 ImageUrl对应于那个按钮:

val imageUrlMap: Map<Int, ImageUrl> = mapOf(
    orangeButton.id to IMAGE_URL_ORANGE,
    redButton.id to IMAGE_URL_RED,
    greenButton.id to IMAGE_URL_GREEN,
)

fun onButtonClick(@Suppress("UNUSED_PARAMETER") v: View) {
    orangeButton.setOnClickListener(::onColoredButtonClicked)
    redButton.setOnClickListener(::onColoredButtonClicked)
    greenButton.setOnClickListener(::onColoredButtonClicked)
}

fun onColoredButtonClicked(button: View) {
    startActivity(Intent(this, ImageActivity::class.java).apply {
        putExtra(IMAGE_BUNDLE_NAME, Bundle().apply {
            putParcelable("key", imageUrlMap[button.id])
        })
    })
}

如果你不想分配一个Map<Int, ImageUrl>我们可以用内联扩展函数和 lambda 做同样的事情:

fun onButtonClick(@Suppress("UNUSED_PARAMETER") v: View) {
    orangeButton.onColoredButtonClicked { IMAGE_URL_ORANGE }
    redButton.onColoredButtonClicked  { IMAGE_URL_RED }
    greenButton.onColoredButtonClicked { IMAGE_URL_GREEN }
}

inline fun Button.onColoredButtonClicked(imageUrlFunc: (Int) -> ImageUrl) {
    startActivity(Intent(this, ImageActivity::class.java).apply {
        putExtra(IMAGE_BUNDLE_NAME, Bundle().apply {
            putParcelable("key", imageUrlFunc())
        })
    })
}

关于android - 如何重构具有 3 个相似方法的 onButtonClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58367912/

相关文章:

安卓 : How to update the selector(StateListDrawable) programmatically

android - Play 商店预发布 : limited to devices that support ArCore even though "com.google.ar.core" is set to "optional"

android - 关闭应用程序后,AIR Android Sound继续播放

android - 如何从 sdcard 中选择一个图像文件,然后如何将它发送到服务器?

java - java方法接受null类数组的kotlin反射

kotlin - 在 Kotlin 1.5 中收集 Flow 时结果嵌套两次(kotlin.Result 不能转换为 `Type`)

android - 在 Recyclerview 中错误地运行 onBindViewHolder - Kotlin

java - 从另一个 Activity Android studio 导入数据

android - 将数据从一个 Activity 中的两个不同 Intent 传递到另一个 Activity

android - 我们如何将 startActivityforResult() 用于电子邮件 Intent ?