我正在制作一个应用程序,其中有 18 个按钮(bAnswer1 到 bAnswer18)和 6 个文本视图(tvAnswer1 到 6)。我的意思是将 6 个单词分成 2 个单词的 3 个单词(例如 button,bu - tt - on),我用 6 个单词做到了这一点,每个单词都将它们放在一个片段中(slice1 到 18),所以这段代码会在 case R 中重复。 id.bAnswer18:你们会注意到这将是休!我知道我可以通过添加一个 for 循环让它变得非常简单,但它不会工作而且我不能出去?!
有人可以帮我解决这个问题吗?
我的代码:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bAnswer1:
if (testing1 != 6) {
String old = tvAnswer1.getText().toString();
tvAnswer1.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
if (testing2 != 6) {
String old = tvAnswer2.getText().toString();
tvAnswer2.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
if (testing3 != 6) {
String old = tvAnswer3.getText().toString();
tvAnswer3.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
if (testing4 != 6) {
String old = tvAnswer4.getText().toString();
tvAnswer4.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
if (testing5 != 6) {
String old = tvAnswer5.getText().toString();
tvAnswer5.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
if (testing6 != 6) {
String old = tvAnswer6.getText().toString();
tvAnswer6.setText(old + slice1);
bAnswer1.setVisibility(View.GONE);
break;
}
break;
case R.id.bAnswer2:
if (testing1 != 6) {
String old = tvAnswer1.getText().toString();
tvAnswer1.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
if (testing2 != 6) {
String old = tvAnswer2.getText().toString();
tvAnswer2.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
if (testing3 != 6) {
String old = tvAnswer3.getText().toString();
tvAnswer3.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
if (testing4 != 6) {
String old = tvAnswer4.getText().toString();
tvAnswer4.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
if (testing5 != 6) {
String old = tvAnswer5.getText().toString();
tvAnswer5.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
if (testing6 != 6) {
String old = tvAnswer6.getText().toString();
tvAnswer6.setText(old + slice2);
bAnswer2.setVisibility(View.GONE);
break;
}
break;