想要把一个String字符串中的每个字符都替换成KeyEvent.KEYCODE_
尝试了下面的方法 但是switch不能带string 切换成JDK1.7整个出错,Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.
switch (sStr)
{
case "a": return KeyEvent.KEYCODE_A;
case "b": return KeyEvent.KEYCODE_B;
case "c": return KeyEvent.KEYCODE_C;
case "d": return KeyEvent.KEYCODE_D;
case "e": return KeyEvent.KEYCODE_E;
case "f": return KeyEvent.KEYCODE_F;
case "g": return KeyEvent.KEYCODE_G;
case "h": return KeyEvent.KEYCODE_H;
case "i": return KeyEvent.KEYCODE_I;
case "j": return KeyEvent.KEYCODE_J;
case "k": return KeyEvent.KEYCODE_K;
case "l": return KeyEvent.KEYCODE_L;
case "m": return KeyEvent.KEYCODE_M;
case "n": return KeyEvent.KEYCODE_N;
想用java的replaceall 结果只能 (string,string) KeyEvent.KEYCODE_属于int
求大神指教啊 急 在线等
可以用反射来获取:
如:
Field field = KeyEvent.class.getDeclaredField("KEYCODE_" + sStr.toUpperCase);
return field.get(null);
这个可以办到你的要求,如果想要优化这个反射的话,可以参考的我博客
http://blog.csdn.net/w172087242/article/details/51648796
如果有帮助,希望结帖