我做了一個應用程序,其中我有18個按鈕(bAnswer1至bAnswer18)和6個文字瀏覽(tvAnswer1至6)。我的意思是用2個3字(如按鈕,bu - tt - on)分割6個單詞,我用6個單詞做了這些單詞,每個單詞放在一個切片中(切片1到18),所以這個代碼會重複自己的情況R. id.bAnswer18:你們會注意到它會是休息!我知道我可以通過添加一個for循環來使它非常簡單,但它不會工作,我不能離開?!Android - 如何把這個循環
有人能幫我擺脫這個嗎?
我的代碼:
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;
只需在它周圍放置一個for循環。正如你所說,你已經知道如何簡化它,所以簡單一點。 – Shark
你應該有一個按鈕數組和另一個textviews數組,而不是6或18個字段。其餘的將是微不足道的。 –
爲什麼你需要在這裏? – TheWhiteRabbit