Android的代码与开关(字符串) [英] Android coding with switch (String)

查看:99
本文介绍了Android的代码与开关(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse上开发一个Android应用程序,当我尝试了一些code像

I'm developing an android app on eclipse, when I try some code like

switch(String){
 ......
}

它给我下面的错误

it give me the following error

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.

那么,有没有反正,让切换(串)工作在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(){}
........

谢谢!

推荐答案

开关语句对象是在Java中1.7引入的新功能。很遗憾的Andr​​oid需要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.

您可以通过存储的方法,这会为某些字符串执行地图上避免使用if语句链; 。你总是可以封装方法用一些处理程序对象。看看这里的更多信息:如何去除大的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

这篇关于Android的代码与开关(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆