单选组按钮验证集在列表视图中选中更改侦听器



对不起,我是安卓新手。需要单选按钮验证方面的帮助。我有两个单选按钮是或否。当我单击提交时,它会为我提供用户选择是或否的值。我想确保用户在单击提交按钮之前应选择任何一个单选按钮是或否。更多细节,而是给我"未尝试",我希望用户只给出是或否。

public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return questionsList.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_items, null);
TextView question = (TextView) view.findViewById(R.id.question);
RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
RadioButton no = (RadioButton) view.findViewById(R.id.no);
RadioGroup rg =(RadioGroup) view.findViewById(R.id.radio_group1);
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "Yes");
}
});
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(i, "No");
}
});

谢谢

只需禁用提交按钮,然后在单选按钮更改侦听器中启用它:

Button button = (Button) findViewById(R.id.button);
button.setEnabled(false); // disabled
button.setEnabled(true);  // enabled

您可以尝试使用复选框而不是两个按钮。

当用户单击提交时,您可以使用 CheckBox.isChecked(( 来定义用户 answear。 如果 isChecked 返回 true,则为"是",否则为"否"。

最新更新