Android coding with switch (String)(带开关的 Android 编码(字符串))
问题描述
当我尝试一些代码时,我正在 Eclipse 上开发一个 android 应用程序
I'm developing an android app on eclipse, when I try some code like
switch(String){
......
}
它给了我以下错误
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
所以我将编译器更改为 1.7,然后它又给了我一个错误
so I changed the compiler to 1.7, then it give me another error
Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.
那么,有没有办法让 switch(String) 在 android 应用程序中工作?或者我别无选择,只能使用
So, is there anyway to let switch(String) works in android application? or I have no choice but to use
if(){}
else if(){}
else if(){}
........
推荐答案
switch
关于String
对象的语句是Java 1.7 中引入的一个新特性.不幸的是,Android 需要 1.6 或 1.5 版本.这就是为什么你必须在一段时间内忘记这种结构.
switch
statement on String
objects is a new feature introduced in Java 1.7. Unfortunatelly Android requires version 1.6 or 1.5. This is why you have to forget for some time about such constructions.
您可以通过存储将为某些 String
执行的方法映射来避免使用 if-statements-chain:Map
.你总是可以用一些 Handler
对象封装 Method
它.在此处查看更多信息:如何删除大型 if-else-if 链
You can avoid using if-statements-chain by storing the map of methods which will be executed for certain String
: Map<String, Method>
. You can always encapsulate Method
it with some Handler
object. Look here for more info: How to remove large if-else-if chain
和
why-cant-i-switch-on-a-string
这篇关于带开关的 Android 编码(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带开关的 Android 编码(字符串)


- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01