硬编码写RadioGroup的时候要注意RadioButton的id重复问题,导致选择的时候出现能够多选的情况发生,如下代码,注意Id的设置,这样避免Radiobutton的id重复。

/**
* 生成单选按钮
*
* @param strTextView
* @param stringList
* @param culName
* @return
*/
private View getRadioGroup(String strTextView, List<String> stringList, String culName,int j) {
String strValues = readFromSharedpreferences(culName);
LayoutInflater inflater = getLayoutInflater();
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.form_radiogroup, null);
TextView textView = (TextView) view.findViewById(R.id.tv_edittext);
textView.setTextColor(Color.BLACK);
textView.setTextSize(18);
textView.setText(strTextView); RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.my_radiogroup);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < stringList.size(); i++) {
RadioButton radioButton = new RadioButton(getApplicationContext());
radioButton.setText(stringList.get(i).toString());
if (strValues != null && strValues.equals(stringList.get(i).toString())) {
hashMap.put(culName, strValues);
radioButton.setChecked(true);
}
radioButton.setId(100*j+i);
radioButton.setTextColor(Color.BLACK);
radioGroup.addView(radioButton); }
view.setTag(radioGroup);
return view;
}
05-01 05:26