我制作的应用程序中有18个按钮(bAnswer1到bAnswer18)和6个文本视图(tvAnswer1到6)。我的意思是用3个单词中的2个单词切换6个单词(比如按钮,按 - 开)我用6个单词做了,每个单词把它们放在一个切片中(slice1到18)所以这段代码将重复自己的情况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;
答案 0 :(得分:0)
你正在寻找的真正答案并非真正处于for循环中。
如果你这样做,for循环将有所帮助
OnClickListener myOnClick = new OnClickListener() {
//privately paste what you had
public void onClick(View v) {
if (testing1 != 6) {
String old = tvAnswer1.getText().toString();
tvAnswer1.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}
if (testing2 != 6) {
String old = tvAnswer2.getText().toString();
tvAnswer2.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}
if (testing3 != 6) {
String old = tvAnswer3.getText().toString();
tvAnswer3.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}
if (testing4 != 6) {
String old = tvAnswer4.getText().toString();
tvAnswer4.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}
if (testing5 != 6) {
String old = tvAnswer5.getText().toString();
tvAnswer5.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}
if (testing6 != 6) {
String old = tvAnswer6.getText().toString();
tvAnswer6.setText(old + slice1);
v.setVisibility(View.GONE);
break;
}});
Button[] answerButtons = new Button[18];
for(Button answer : answerButtons)
answer.setOnClickListener(myOnClick);
请记住,不要在Activity上实现OnClickListener,让视图本身处理它,除非你真的需要自己处理/发送onClicks。