【问题标题】:How to access a variable inside when statement in Kotlin如何在 Kotlin 中的 when 语句中访问变量
【发布时间】:2020-03-30 08:43:05
【问题描述】:

我正在开发一项自我评估功能,这将使人们知道他们是否有感染电晕的风险,从而建议他们是否接受检测。我创建了许多单选按钮,让患者检查不同的症状,为每个单击的单选按钮分配,该单选按钮应该与其他值相加,以便找到一个分数,我们可以在该分数上知道患者是否应该是否继续进行covid-19测试。我需要帮助来修复我的代码,以便能够读取和添加每个单选按钮的所有值。这是我的代码。

if (view is RadioButton) {
        // Is the button now checked?
        val checked = view.isChecked

        // Check which radio button was clicked
        when (view.getId()) {
            R.id.yes_1 ->
                if (checked) {
                    val yes1= 1
                    Toast.makeText(this, "Yes 1", Toast.LENGTH_LONG).show()
                }
            R.id.no_1 ->
                if (checked){
                    val no1=0
                    Toast.makeText(this, " No 1", Toast.LENGTH_SHORT).show()
                }
            R.id.yes_2 ->
                if (checked) {
                    val yes2= 1
                    Toast.makeText(this, "Yes 2", Toast.LENGTH_LONG).show()
                }
            R.id.no_2 ->
                if (checked) {
                    val no2= 0
                    Toast.makeText(this, "No 2", Toast.LENGTH_SHORT).show()
                }
            R.id.yes_3 ->
                if (checked) {
                    val yes3=1
                    Toast.makeText(this, "Yes 3", Toast.LENGTH_LONG).show()
                }
            R.id.no_3 ->
                if (checked) {
                    val no3=0
                    Toast.makeText(this, "No 3", Toast.LENGTH_SHORT).show()
                }
            R.id.yes_4 ->
                if (checked) {
                    val yes4=1
                    Toast.makeText(this, "Yes 4", Toast.LENGTH_LONG).show()
                }
            R.id.no_4 ->
                if (checked) {
                    val no4=0
                    Toast.makeText(this, "No 4", Toast.LENGTH_SHORT).show()
                }
            R.id.yes_5 ->
                if (checked) {
                    val yes5=5
                    Toast.makeText(this, "Yes 5", Toast.LENGTH_LONG).show()
                }
            R.id.no_5 ->
                if (checked) {
                    val no5=0
                    Toast.makeText(this, "No 5", Toast.LENGTH_SHORT).show()
                }
            R.id.yes_6 ->
                if (checked) {
                    val yes6=5
                    Toast.makeText(this, "Yes 6", Toast.LENGTH_LONG).show()
                }
            R.id.no_6 ->
                if (checked) {
                    val no6=0
                    Toast.makeText(this, "No 6", Toast.LENGTH_SHORT).show()
                }
        }
        val assesment_point= yes1 + n01 + yes2 + no2 +yes3 + no3 + yes4 + no4 + yes5 + no5 + yes6 + no6
    }
}

Here is the screenshot of how the app works

【问题讨论】:

    标签: android-studio kotlin android-activity switch-statement


    【解决方案1】:

    简短的回答是scope 定义是否可以访问变量。在您的情况下,这些 val 仅存在于 when case 块中,而不存在于外部。

    快速修复可能是在外部预定义它们并仅更改 when 块内的值。

    (offtopic:您误解了radio button sample 一点。在您的情况下,您不想对每个单选按钮单击做出反应,但是一旦按下“提交”按钮。您也不需要“yes6”只是一个“answer6”,“0”表示否或“1”表示是。这已经会大大降低代码复杂性......)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多