【问题标题】:Kotlin multiple when statementKotlin 多重 when 语句
【发布时间】:2021-12-30 11:55:15
【问题描述】:

我正在学习使用 android studio 构建一个简单的 android 应用程序,并且我创建了一个函数来查找某些值的 id。在编写这个函数时,我想使用 when 语句(Kotlin),但遗憾的是不得不重复它。有没有办法将 when 语句的结果同时分配给多个变量?在其他语言中,我只会返回一个我会反汇编的列表,但我找不到在 Kotlin 中执行此操作的方法。这不是什么大问题,但我喜欢优化我的代码。

// my Kotlin function
// setting a specific state
private fun setState(num: Int) {
    Log.v(TAG, num.toString())
    // get the correct image id
    val imageId: Int? = when (num) {
        0 -> R.drawable.lemon_restart
        1 -> R.drawable.lemon_tree
        2 -> R.drawable.lemon_squeeze
        3 -> R.drawable.lemon_drink
        else -> null
    }
    // get the correct text to show
    val txtId: Int? = when (num) {
        0 -> R.string.txt_state_0
        1 -> R.string.txt_state_1
        2 -> R.string.txt_state_2
        3 -> R.string.txt_state_3
        else -> null
    }
    // get the correct content description for accessibility
    val contentDescriptionId: Int? = when (num) {
        0 -> R.string.lemon_restart_description
        1 -> R.string.lemon_tree_description
        2 -> R.string.lemon_squeeze_description
        3 -> R.string.lemon_drink_description
        else -> null
    }
    // setting the new stuff
    val imView: ImageView = findViewById(R.id.imageState)
    val txtView: TextView = findViewById(R.id.textOrder)
    txtView.text = getString(txtId!!)
    imView.setImageResource(imageId!!)
    imView.contentDescription = getString(contentDescriptionId!!)
}

尽量优化

【问题讨论】:

  • feel free to optimize it as much as possible 你真的相信在一个函数中拥有所有这些就可以了吗?似乎这个函数只负责一件事而且只负责一件事,它做得太多了

标签: android kotlin variables optimization keyword


【解决方案1】:

你可以从when返回Triple或者你自己的数据类,然后destructure它:

val (imageId, txtId, contentDescriptionId) = when (num) {
    0 -> Triple(R.drawable.lemon_restart, R.string.txt_state_0, R.string.lemon_restart_description)
    ...
    else -> Triple(null, null, null)
}

【讨论】:

  • 这正是我想要的。谢谢!
【解决方案2】:

因为每个字段都是恒定的并且状态是固定的。您可以使状态保持不变。要稍微解耦代码,您可以创建一个单独的类来返回特定状态的值。下面是一个例子:

class StateHandle private constructor(imageId: Int?, txtId: Int?, contentDescriptionId: Int?) {
    companion object {
        private val imageIds = arrayOf(
            R.drawable.lemon_restart,
            R.drawable.lemon_tree,
            R.drawable.lemon_squeeze,
            R.drawable.lemon_drink
        )
        private val txtIds = arrayOf(
            R.string.txt_state_0,
            R.string.txt_state_1,
            R.string.txt_state_2,
            R.string.txt_state_3
        )
        private val contentIds = arrayOf(
            R.string.lemon_restart_description,
            R.string.lemon_tree_description,
            R.string.lemon_squeeze_description,
            R.string.lemon_drink_description
        )

        @JvmStatic
        fun getStateFor(num: Int): StateHandle {
            return StateHandle(
                imageIds.getOrNull(num), txtIds.getOrNull(num),
                imageIds.getOrNull(num)
            )
        }
    }
}

它并不完美,但它更易于重复使用。只需调用#getStateFor 并使用StateHandle 对象。

【讨论】:

  • 哇,这是一个非常有趣的解决方案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-08-19
  • 2022-08-16
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2022-07-08
  • 2019-05-05
相关资源
最近更新 更多