【发布时间】:2011-11-01 18:10:29
【问题描述】:
我的应用程序有一个问题列表。当调用 getfirstquestion 函数时,它会随机选择一个问题,然后在 4 个按钮中的每一个上随机放置文本(其中一个是问题的答案)。
每次按下按钮时,函数 checkanswer 会验证按钮文本是否等于正确问题的正确答案。如果是,则删除当前问题,并调用 getfirstquestion() 函数来更改问题。
程序运行正常,但是当我按下正确答案按钮时没有任何反应。谁能告诉我为什么,并给我一个解决方案?我从 2 小时前开始搜索代码....
public class startgame extends Activity implements OnClickListener{
final Random rgenerator = new Random();
List<String> questionss1 = new ArrayList<String>();
String thequestion;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
questionss1.add("Who is the actual CEO at Apple?");
questionss1.add("Who is the actual CEO at Microsoft?");
questionss1.add("Who is the actual CEO at Google?");
getfirstquestion();
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
Button button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(this);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button1:
Button button1 = (Button)findViewById(R.id.button1);
checkanswer((String) button1.getText());
case R.id.button2:
Button button2 = (Button)findViewById(R.id.button2);
checkanswer((String) button2.getText());
case R.id.button3:
Button button3 = (Button)findViewById(R.id.button2);
checkanswer((String) button3.getText());
case R.id.button4:
Button button4 = (Button)findViewById(R.id.button2);
checkanswer((String) button4.getText());
}
}
public int checkanswer(String buttontext) {
if (thequestion.equals("Who is the actual CEO at Apple?") && buttontext == "Tim Cook"){
questionss1.remove("Who is the actual CEO at Apple?");
getfirstquestion();
}
if (thequestion.equals("Who is the actual CEO at Microsoft?") && buttontext == "Steve Ballmer"){
questionss1.remove("Who is the actual CEO at Microsoft?");
getfirstquestion();
}
if (thequestion.equals("Who is the actual CEO at Google?") && buttontext == "Eric Schmidt"){
questionss1.remove("Who is the actual CEO at Google?");
getfirstquestion();
}
return 0;
}
public void getfirstquestion(){
//create the buttons
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
thequestion = questionss1.get(rgenerator.nextInt(questionss1.size()));
TextView question = (TextView)findViewById(R.id.textView1);
question.setText(thequestion);
questionss1.remove(thequestion);
if (thequestion.equals("Who is the actual CEO at Apple?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
if (thequestion.equals("Who is the actual CEO at Microsoft?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
if (thequestion.equals("Who is the actual CEO at Google?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
}
}
【问题讨论】:
标签: android string button arraylist