基本上我想在可绘制资源之间切换,我无法弄清楚我将如何去做。我在一个If语句中写了所有内容,但它太长而且凌乱,而且它没有按照我想要的方式执行,因为如果输入错误的字母它就不会停止。
我要做的是,如果用户输入一个字母,背景将更改为之后的下一个字母,例如,如果用户输入A,则背景将更改为B,依此类推。它现在可以工作,但是错误的是如果用户在字母A上并且用户错误地输入字母P,则背景将切换到Q而不是告诉用户他们错了。
我确实有这个代码作为其他而不仅仅是If语句,但我想我今天会改变它。任何人都可以解释我如何将这个If语句放入案例陈述中吗?谢谢你的帮助
if (words.getText().toString().equalsIgnoreCase("A")) {
gestureViewer.setBackgroundResource(R.drawable.lettersb);
} else {
onDestroy();
}
if (words.getText().toString().equalsIgnoreCase("B")) {
gestureViewer.setBackgroundResource(R.drawable.lettersc);
} else {
}
if (words.getText().toString().equalsIgnoreCase("C")) {
gestureViewer.setBackgroundResource(R.drawable.lettersd);
} else {
}
if (words.getText().toString().equalsIgnoreCase("D")) {
gestureViewer.setBackgroundResource(R.drawable.letterse);
} else {
}
if
(words.getText().toString().equalsIgnoreCase("E")) {
gestureViewer.setBackgroundResource(R.drawable.lettersf);
} else {
}
if (words.getText().toString().equalsIgnoreCase("F")) {
gestureViewer.setBackgroundResource(R.drawable.lettersg);
} else {
}
if (words.getText().toString().equalsIgnoreCase("G")) {
gestureViewer.setBackgroundResource(R.drawable.lettersh);
}else {
}
if (words.getText().toString().equalsIgnoreCase("H")) {
gestureViewer.setBackgroundResource(R.drawable.lettersi);
}else {
}
if (words.getText().toString().equalsIgnoreCase("I")) {
gestureViewer.setBackgroundResource(R.drawable.lettersj);
} else {
}
if (words.getText().toString().equalsIgnoreCase("J")) {
gestureViewer.setBackgroundResource(R.drawable.lettersk);
}else {
}
if (words.getText().toString().equalsIgnoreCase("K")) {
gestureViewer.setBackgroundResource(R.drawable.lettersl);
}else {
}
if (words.getText().toString().equalsIgnoreCase("L")) {
gestureViewer.setBackgroundResource(R.drawable.lettersm);
}else {
}
if (words.getText().toString().equalsIgnoreCase("M")) {
gestureViewer.setBackgroundResource(R.drawable.lettersn);
}else {
}
if (words.getText().toString().equalsIgnoreCase("N")) {
gestureViewer.setBackgroundResource(R.drawable.letterso);
}else {
}
if (words.getText().toString().equalsIgnoreCase("O")) {
gestureViewer.setBackgroundResource(R.drawable.lettersp);
}else {
}
if (words.getText().toString().equalsIgnoreCase("P")) {
gestureViewer.setBackgroundResource(R.drawable.lettersq);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Q")) {
gestureViewer.setBackgroundResource(R.drawable.lettersr);
} else {
}
if (words.getText().toString().equalsIgnoreCase("R")) {
gestureViewer.setBackgroundResource(R.drawable.letterss);
} else {
}
if (words.getText().toString().equalsIgnoreCase("S")) {
gestureViewer.setBackgroundResource(R.drawable.letterst);
}else {
}
if (words.getText().toString().equalsIgnoreCase("T")) {
gestureViewer.setBackgroundResource(R.drawable.lettersu);
}else {
}
if (words.getText().toString().equalsIgnoreCase("U")) {
gestureViewer.setBackgroundResource(R.drawable.lettersv);
}else {
}
if (words.getText().toString().equalsIgnoreCase("V")) {
gestureViewer.setBackgroundResource(R.drawable.lettersw);
}else {
}
if (words.getText().toString().equalsIgnoreCase("W")) {
gestureViewer.setBackgroundResource(R.drawable.lettersx);
}else {
}
if (words.getText().toString().equalsIgnoreCase("X")) {
gestureViewer.setBackgroundResource(R.drawable.lettersy);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Y")) {
gestureViewer.setBackgroundResource(R.drawable.lettersz);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Z")) {
gestureViewer.setBackgroundResource(R.drawable.lettersa);
} else {
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void afterTextChanged(Editable s) {
}
});
}
答案 0 :(得分:2)
使用以下
char ch=words.getText().toString().charAt(0));
switch(ch-'A')
{
case 0:
//A;
break;
case 1:
//B;
break;
.....
}
通过这个你可以将if语句转换成switch case:
你也可以拥有可绘制阵列,然后你也不需要使用开关盒:
int[] arr=new int[]{R.id.drawable_A, .......};
然后设置颜色
char ch=words.getText().toString().charAt(0));
int index=ch-'A';
view.setBackgroundResource(index);
答案 1 :(得分:1)
switch (words.getText().toString().charAt(0)) {
case 'A':
gestureViewer.setBackgroundResource(R.drawable.lettersb);
break;
case 'B' :
gestureViewer.setBackgroundResource(R.drawable.lettersc);
break;
答案 2 :(得分:1)
private void set( i) {
switch(i) {
case 1:
words.getText().toString().equalsIgnoreCase("A")) {
gestureViewer.setBackgroundResource(R.drawable.lettersf);
break;
case 2:
words.getText().toString().equalsIgnoreCase("B")) {
gestureViewer.setBackgroundResource(R.drawable.lettersf);
break;
.
.......
}
}
答案 3 :(得分:0)
这可能是一种更好的方法
char current = words.getText().toString().charAt(0));
char next = ( (ch-'A') + 1 ) % 26 + 'A';
gestureViewer.setBackgroundResource(R.drawable.getLetter( next ));
接下来是字母表中的[循环]下一个字符。
祝你好运