Android 如何使用RadioButton展开布局,然后添加到RadioGroup?
我正在尝试为一个具有两个TextView和一个RadioButton的行的Listview扩展布局。我希望所有以这种方式添加的单选按钮都在同一个放射组中。以下是我迄今为止所做的工作:Android 如何使用RadioButton展开布局,然后添加到RadioGroup?,android,radio-button,radio-group,Android,Radio Button,Radio Group,我正在尝试为一个具有两个TextView和一个RadioButton的行的Listview扩展布局。我希望所有以这种方式添加的单选按钮都在同一个放射组中。以下是我迄今为止所做的工作: @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; if (v =
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.config_grid_item, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.config_item1);
holder.item2 = (TextView) v.findViewById(R.id.config_item2);
holder.item3 = (RadioButton) v.findViewById(R.id.config_item3);
group.addView(holder.item3);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Profile profile = profiles.get(position);
if (profile != null) {
holder.item1.setText(profile.IP);
holder.item2.setText(profile.name);
}
return v;
}
和XML行:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:minHeight="60dip">
<TextView android:id="@+id/config_item2" android:text="Name"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center_vertical" android:width="130dip"
android:padding="10dip" />
<TextView android:id="@+id/config_item1" android:text="IP"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center_vertical" android:width="130dip"
android:padding="10dip" />
<RadioButton android:id="@+id/config_item3" android:width="40dip"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:checked="false" android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
从错误日志中,我可以猜测,因为RadioButton已经是行xml中LinearLayout的子项,所以它不希望添加到radioGroup。你知道怎么解决这个问题吗?我基本上只想一次选择一个单选按钮,这就是为什么我要将它们添加到一个radiogroup中
编辑:我刚刚有了一个天才的想法,即收听错误消息并尝试从LinearLayout中删除视图。它没有崩溃,但是现在单选按钮没有出现在我的行中。微小的进展?以下是我添加/更改的内容:
LinearLayout test = (LinearLayout) v.findViewById(R.id.LinearRadio);
test.removeView(holder.item3);
group.addView(holder.item3);
不能同时向两个布局添加视图。
阅读编辑后:是,您将无法查看该单选按钮,因为它已从其可见父按钮中删除并添加到不可见的放射组中
似乎您需要一个具有singlechoice的listview,每行有两行<代码>listView.setChoiceMode(listView.CHOICE\u MODE\u SINGLE)代码>项目行为
简单列表\u项目2
将满足需要。您需要将无父线性布局添加到放射组
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.config_grid_item, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.config_item1);
holder.item2 = (TextView) v.findViewById(R.id.config_item2);
holder.item3 = (RadioButton) v.findViewById(R.id.config_item3);
group.addView(v);
不是很好的解决方案…只需取消选中所有单选按钮。(根本不需要放射组)
private void uncheckRadioBtn(){for(int i=0;i
LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.config_grid_item, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.config_item1);
holder.item2 = (TextView) v.findViewById(R.id.config_item2);
holder.item3 = (RadioButton) v.findViewById(R.id.config_item3);
group.addView(v);