【发布时间】:2012-04-19 12:19:51
【问题描述】:
我正在尝试制作一个测验应用程序,因此有 5 个单选按钮提供可能的答案,只有 1 个是正确的。然后是一个提交按钮,它有一个 onClick="clickMethod" 来处理提交。
我的 clickMethod 看起来像这样:
public void clickMethod(View v){
RadioGroup group1 = (RadioGroup) findViewById(R.id.radioGroup1);
int selected = group1.getCheckedRadioButtonId();
RadioButton button1 = (RadioButton) findViewById(selected);
if (button1.getText()=="Right Answer")
Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show();
}
但是无论如何我都无法让 IF 语句工作。如果我试着用 “button1.getText()”作为参数,它确实打印了“正确答案”字符串,但由于某种原因在 IF 语句中它不起作用,即使我检查了正确答案,ELSE 也会始终执行。
有谁知道可能发生的事情或更好的方法吗?
【问题讨论】: