列表视图中的单选按钮验证setOnCheckedChangeListener

时间:2018-06-15 00:31:14

标签: android

我们很抱歉我是android的新手。在单选按钮验证方面需要帮助。我有两个单选按钮是或否。当我点击提交时,它给出了用户选择是或否的价值。我想确保用户在点击提交按钮之前应该选择任何一个单选按钮是或否。更多细节反而给予我"未尝试"我希望用户只给出是或否。

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");

        }
    });

谢谢

2 个答案:

答案 0 :(得分:1)

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

false

答案 1 :(得分:0)

您可以尝试使用checkbox而不是两个按钮。

当用户点击提交时,您使用CheckBox.isChecked()来定义用户安装。 如果isChecked返回true,则返回yes,否则返回no。

相关问题