我如何使用 3来调整这个示例Radiobuttons
我想获取RadioButtons
我的列表适配器中选择的所有内容。
问问题
177 次
2 回答
0
布局文件应该是:
<RadioGroup
android:id="@+id/grp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op1" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op2" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op3" />
</RadioGroup>
ArrayAdapter 应该是:
private class ViewHolder {
protected TextView text;
protected RadioGroup radioGroup;
}
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
HashMap<Integer, RadioButton> mapRadioBtnsSelected;
ArrayList<View> arrViews;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
mapRadioBtnsSelected = new HashMap<Integer, RadioButton>();
arrViews = new ArrayList<View>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.grp);
viewHolder.radioGroup.setTag(position);
convertView.setTag(viewHolder);
arrViews.add(convertView);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
viewHolder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
int getPosition = (Integer) group.getTag();
if(mapRadioBtnsSelected.containsKey(getPosition))
mapRadioBtnsSelected.remove(getPosition);
mapRadioBtnsSelected.put(getPosition, arrViews.get(getPosition).findViewById(checkedId);
}
}
});
return convertView;
}
}
public class Model {
private String name;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
mapRadioBtnsSelected 包含选定单选按钮的列表。
于 2013-11-20T12:55:11.550 回答
0
像这样创建一个广播组。
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioChild1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild1"
android:checked="true" />
<RadioButton
android:id="@+id/radioChild2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild2" />
<RadioButton
android:id="@+id/radioChild3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild3" />
</RadioGroup>
将此布局膨胀到您的自定义适配器
用于setOnCheckedChangeListener
从列表中获取选定的值。
ArrayList<String> selectedCheckBox = new ArrayList<String>();
YourView.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
selectedCheckBox.add(values);
}
else
{
selectedCheckBox.remove(values);
}
}
});
有关更多详细信息,请参阅此链接
于 2013-11-11T07:53:57.350 回答