Android coding with switch (String)

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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 

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. 

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(){} ........ 

回答1:

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.

You can avoid using if-statements-chain by storing the map of methods which will be executed for certain String: Map. You can always encapsulate Method it with some Handler object. Look here for more info: How to remove large if-else-if chain

and

why-cant-i-switch-on-a-string



回答2:

With the Release of API 19 KitKat you can now do this !!!! This is the change you need for android studio for existing projects. ensure that you are using JDK 1.7 or later and Gradle plugin 0.6.1 or later

android { compileSdkVersion 19 buildToolsVersion "19.0.0"  defaultConfig {     minSdkVersion 7     targetSdkVersion 19 }  compileOptions {     sourceCompatibility JavaVersion.VERSION_1_7     targetCompatibility JavaVersion.VERSION_1_7 } } 


回答3:

You have no choice. You can't work Switch with Strings (at least for now). Following message explicitly conforms that.

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead



回答4:

You can do this with the latest android build tools. Android started supporting Java 7 feature since API 19.

The best part is that it will work even if you set your minSdkVersion lower than 19. Quoting this android developer page:

Note that you can use minSdkVersion with a value earlier than 19, for all language features except try with resources. If you want to use try with resources, you will need to also use a minSdkVersion of 19.



回答5:

Here is other alternative for android with java 5 or 6

public enum Command  {text, image, other};  String enumTest = "text"; try{     Command command  = Command.valueOf(enumTest); } catch(Exception e){     Log.e("Tag", "Command not found: " + enumTest); } switch (command){    case image:       break;    case  text:        break; } 


回答6:

Since I assume most people are using Android Studio with at least a minimum of Java 7, then the answer now is, Yes, you can switch Strings in Android, even for minSdkVersion versions Java SE Documentation:

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {      String typeOfDay;      switch (dayOfWeekArg) {          case "Monday":              typeOfDay = "Start of work week";              break;          case "Tuesday":          case "Wednesday":          case "Thursday":              typeOfDay = "Midweek";              break;          case "Friday":              typeOfDay = "End of work week";              break;          case "Saturday":          case "Sunday":              typeOfDay = "Weekend";              break;          default:              throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);      }      return typeOfDay; } 

The docs also note:

  • Using string switching is generally faster than chained if-else.
  • It is as if each case were using String.equals.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!