【问题标题】:How to refactor onButtonClick having 3 similar methods?如何重构具有 3 个类似方法的 onButtonClick?
【发布时间】:2019-10-13 21:22:43
【问题描述】:

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

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?

【问题讨论】:

    标签: android kotlin android-intent android-activity refactoring


    【解决方案1】:

    这是实现它的一种方式。基本上我们使用按钮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&lt;Int, ImageUrl&gt;,我们可以使用内联扩展函数和 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())
            })
        })
    }
    

    【讨论】:

    • onButtonClick(...) 应该是setupClickHandlers(...) 吗?它看起来很像一个点击处理程序本身。也许该代码应该在onCreate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2021-07-16
    相关资源
    最近更新 更多