什么是的语法使用时数comparations在当的声明

0

的问题

我有这个代号:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            320 -> StatisticsSettings.SMALL_PHONE
            480 -> StatisticsSettings.LARGE_PHONE
            600 -> StatisticsSettings.SMALL_TABLET
            720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }

我想知道如果我可以让情况下的 when 声明与比较,而不是一个整数。 事情是这样的:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 320 -> StatisticsSettings.SMALL_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 480 -> StatisticsSettings.LARGE_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 600 -> StatisticsSettings.SMALL_TABLET
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }
kotlin
2021-11-23 23:05:51
1

最好的答案

4

使用范围:

val statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
  in 0..320 -> StatisticsSettings.SMALL_PHONE
  in 321..480 -> StatisticsSettings.LARGE_PHONE
  in 481..600 -> StatisticsSettings.SMALL_TABLET
  in 601..720 -> StatisticsSettings.LARGE_TABLET
  else -> throw IllegalArgumentException("Cannot compute dp")
}

或者你可以使用枚举constants:

enum class StatisticsSettings(val intRange: IntRange) {
  SMALL_PHONE(0..320),
  LARGE_PHONE(321..480),
  SMALL_TABLET(481..600),
  LARGE_TABLET(601..720)
}

val intRange = ScreenHandler.convertPixelsToDp(width, context).toInt()

val statisticsSettings = StatisticsSettings.values().find { intRange in it.intRange }

这具有的优点是,范围是"约束"enum本身。 如果你改变了这些价值观,你不必改变它们关于可能的多个地点在你的代码。

编辑:交换来自 过滤 ,以 找到 (由于@ArpitShukla,参见下面的评论)

2021-11-24 08:43:33

你可以替换 filterfind. 这将使更多的意义在这里。
Arpit Shukla

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................