我有一个情况需要从动态创建的单选按钮中选择一个。我知道使用Radio group是可能的,但由于我的情况有些不同,我仍然无法做到。让我解释一下。
我有一个问题:“最近的调查发现,在英国的中学中”,有4个选项,如 A。 B。 C。 D。
所有数据都以json形式从服务器传输。我已经动态创建了一个包含一个问题和四个带单选按钮的选项的视图。但我理解的是每一行都是新行,我无法只从所有单选按钮中选择一个单选按钮。每个按钮都是单独选择的,但我想要在我点击的单选按钮上选择一个单选按钮。
任何帮助将不胜感激。
您可以在下面看到我的视图:
我有一个问题:“最近的调查发现,在英国的中学中”,有4个选项,如 A。 B。 C。 D。
所有数据都以json形式从服务器传输。我已经动态创建了一个包含一个问题和四个带单选按钮的选项的视图。但我理解的是每一行都是新行,我无法只从所有单选按钮中选择一个单选按钮。每个按钮都是单独选择的,但我想要在我点击的单选按钮上选择一个单选按钮。
任何帮助将不胜感激。
您可以在下面看到我的视图:
我的代码是:
LinearLayout linearLayoutForQuestions = (LinearLayout) view.findViewById(R.id.questionsLinearLayout);
linearLayoutForQuestions.removeAllViews();
//set the questions for the user
for (int ss = 0; ss < totalNoOfQuestions.size(); ss++) {
final List<String> list = new ArrayList();
list.add("SELECT");
TextView textView = new TextView(activity);
textView.setTextSize(15);
textView.setTextColor(view.getResources().getColor(R.color.black));
textView.setTypeface(typeface1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 20, 0, 0);
textView.setLayoutParams(params);
String question = totalNoOfQuestions.get(ss).getQuestions();
String questionNo = totalNoOfQuestions.get(ss).QuestionNo;
textView.setText("Question " + questionNo + " " + question);
//linearlayout and set data inside it
LinearLayout parent = new LinearLayout(activity);
parent.removeAllViews();
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.VERTICAL);
int optionSize = totalNoOfQuestions.get(ss).getOptions().size();
for (int s = 0; s < optionSize; s++) {
//children of parent linearlayout
LinearLayout layout2 = new LinearLayout(activity);
layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setGravity(Gravity.CENTER);
String optionNo = totalNoOfQuestions.get(ss).getOptions().get(s).getQuestionOptionNo();
TextView textView1 = new TextView(activity);
textView1.setText(optionNo);
textView.setTextSize(15);
final RadioButton radioButton = new RadioButton(activity.getApplicationContext());
radioButton.setId(s);
radioButton.setBackgroundResource(R.drawable.settings_background_ripple);
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
pa.setMargins(10,10,10,10);
radioButton.setLayoutParams(pa);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int checkedRadioButtonId = buttonView.getId();
Toast.makeText(activity, "hit it", Toast.LENGTH_SHORT).show();
}
});
String questionOption = totalNoOfQuestions.get(ss).getOptions().get(s).getQuestionOption();
TextView textView2 = new TextView(activity);
textView2.setTextSize(15);
textView2.setText(questionOption);
layout2.addView(textView1);
layout2.addView(radioButton);
layout2.addView(textView2);
parent.addView(layout2);
}
linearLayoutForQuestions.addView(textView);
linearLayoutForQuestions.addView(parent);
}