如何在 android 中的测验应用程序中禁用单选组中的单选按钮
How to disable radio button in a radio group in quiz application in android
我开发了一个基于测验的 android 应用程序,我在其中从数组中加载问题和答案一切正常,但我面临的唯一问题是在为特定问题选择一个选项并提交之后对于下一个问题,所选选项仍然处于选中状态,我也需要清除选中的选项。这是我试过的
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton)findViewById(rg1.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
//Toast.makeText(getApplicationContext(), ""+ansText, 5000).show();
if(ansText.equalsIgnoreCase(answers[flag]))
{
correct++;
}
else
{
wrong++;
}
//Toast.makeText(getApplicationContext(), "Flag before INCR==> " + flag, 5000).show();
flag++;
if(flag < ques.length)
{
//Toast.makeText(getApplicationContext(), "Flag after INCR==> " + flag, 5000).show();
rg1.setEnabled(false);
t2.setText(ques[flag]);
r1.setText(options[flag*4]);
r2.setText(options[flag*4 + 1]);
r3.setText(options[flag*4 + 2]);
r4.setText(options[flag*4 + 3]);
}
尝试uans.setChecked(假); - 从您的代码中,这是从组 (rg1) 中获取选中的单选按钮。
Tip:如果您需要自己更改单选按钮状态(例如加载保存的 CheckBoxPreference 时),请使用 setChecked(boolean) 或 toggle() 方法。
您可以将所有 RadioButtons
放在一个 RadioGroup then when you move to the next question just clear them all with clearCheck() 方法中 (RadioGroup.clearCheck();
)
我开发了一个基于测验的 android 应用程序,我在其中从数组中加载问题和答案一切正常,但我面临的唯一问题是在为特定问题选择一个选项并提交之后对于下一个问题,所选选项仍然处于选中状态,我也需要清除选中的选项。这是我试过的
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton)findViewById(rg1.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
//Toast.makeText(getApplicationContext(), ""+ansText, 5000).show();
if(ansText.equalsIgnoreCase(answers[flag]))
{
correct++;
}
else
{
wrong++;
}
//Toast.makeText(getApplicationContext(), "Flag before INCR==> " + flag, 5000).show();
flag++;
if(flag < ques.length)
{
//Toast.makeText(getApplicationContext(), "Flag after INCR==> " + flag, 5000).show();
rg1.setEnabled(false);
t2.setText(ques[flag]);
r1.setText(options[flag*4]);
r2.setText(options[flag*4 + 1]);
r3.setText(options[flag*4 + 2]);
r4.setText(options[flag*4 + 3]);
}
尝试uans.setChecked(假); - 从您的代码中,这是从组 (rg1) 中获取选中的单选按钮。
Tip:如果您需要自己更改单选按钮状态(例如加载保存的 CheckBoxPreference 时),请使用 setChecked(boolean) 或 toggle() 方法。
您可以将所有 RadioButtons
放在一个 RadioGroup then when you move to the next question just clear them all with clearCheck() 方法中 (RadioGroup.clearCheck();
)