在动态 LinearLayout 中添加 RadioGroup
Add RadioGroup in dynamic LinearLayout
我在调用 parentLinearLayout
的 LinearLayout 中为应用程序动态生成单选按钮(例如:3 个单选按钮)。问题是,它们不会自动成组,所以如果我选中一个按钮然后再选中另一个按钮,则两者都选中而不是取消选中第一个按钮。所以,我想我应该做一个 RadioGroup。
该应用本身是一个问卷应用,每个问题都有不同数量的可能答案。我从单选按钮布局中将答案数创建为单选按钮。
问题是,我收到一条错误消息,说我应该在这样做之前删除旧视图,但是如果我这样做,我会丢失生成的线性布局:The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的代码:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
for (int i=0;i<drawables;i++){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.radiobutton, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); <== HERE IS THE PROBLEM
rd.setText(current.getAnswers().get(i).getAns());
}
}
有什么办法可以解决这个问题吗?谢谢
- 您正在循环中的每次迭代中创建新布局。
- 通过膨胀新视图,您正在丢失旧视图
- 每次您使用相同的 ID
R.id.radiobtn
查找视图并在布局中再次添加相同的视图时,这是不可能的,因此会出现问题。
for (int i=0;i<drawables;i++){
// 1,2
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//
final View rowView = inflater.inflate(R.layout.radiobutton, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); // same view with same id R.id.radiobtn
rd.setText(current.getAnswers().get(i).getAns());
}
解决方案:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
// create a radio group as container with direction
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
// create radio button, with id and text
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
// add radio button into group
l1.addView(rd);
}
// finally, add radio group with buttons into parent layout
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
你已经很接近了,我想只要改变一些线路就可以了。
public void drawRAnswers(int pst){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
for (int i=0;i<drawables;i++){
final View rowView = inflater.inflate(R.layout.radiobutton, null);
// Add the new row before the add field button.
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
rd.setText(current.getAnswers().get(i).getAns());
rd.setId(i);
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
//or
parentLinearLayout.addView(l1);
}
RadioGroups
是幕后美化的 LinearLayouts,这意味着它们可以支持 any children,但只会处理 RadioButtons
如果他们是 组 的直接 children,则为相同的“组”。例如:
想象一下这个层次结构:
<RadioGroup> // THIS YOU HAVE IN XML
<RadioButton 1> // ALL THESE YOU CREATE PROGRAMMATICALLY
<RadioButton 2>
<RadioButton 3>
</RadioGroup>
所以你的 XML 看起来像:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/your_radio_button"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Radio Buttons will be programmatically added here -->
</RadioGroup>
所以……您以编程方式创建所有内容,这很好。你有一个“循环”,比如……
final RadioGroup group = //find your_radio_button in the layout.
// now iterate
for (Something something : listOfSomethings) {
final RadioButton rb = new RadioButton(context);
rb.setId(View.generateViewId());
rb.setText(something.giveMeTheText());
rb.setLayoutParams(layoutParams); // make these depending what your container for the RadioGroup is.
rb.setOnCheckedChangeListener(your OnCheckedChangeListener);
group.add(rb);
}
如果你需要监听变化,显然添加一个普通的CompoundButton.OnCheckedChangeListener
并覆盖:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do what you need ;)
}
请记住,如果按钮有 onCheckedChangeListener
,那么在您执行 group.add(rb)
的那一刻,它将被触发……如果您将其设置为已选中(我认为即使您不这样做,它也会触发't with a “false”),所以你可能想延迟监听器的分配,直到你完成向 RadioGroup 添加按钮,然后迭代它们并同时添加它们(这不会触发回调就在那里,因为按钮已经在层次结构中并且没有改变)。
如果你需要插入一些东西 other 而不是 RadioButton 作为你 RadioGroup
的直接 child 那是另一个问题的主题(这是可行的,不是自动的,需要你做一些 extra-work)。
我在调用 parentLinearLayout
的 LinearLayout 中为应用程序动态生成单选按钮(例如:3 个单选按钮)。问题是,它们不会自动成组,所以如果我选中一个按钮然后再选中另一个按钮,则两者都选中而不是取消选中第一个按钮。所以,我想我应该做一个 RadioGroup。
该应用本身是一个问卷应用,每个问题都有不同数量的可能答案。我从单选按钮布局中将答案数创建为单选按钮。
问题是,我收到一条错误消息,说我应该在这样做之前删除旧视图,但是如果我这样做,我会丢失生成的线性布局:The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的代码:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
for (int i=0;i<drawables;i++){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.radiobutton, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); <== HERE IS THE PROBLEM
rd.setText(current.getAnswers().get(i).getAns());
}
}
有什么办法可以解决这个问题吗?谢谢
- 您正在循环中的每次迭代中创建新布局。
- 通过膨胀新视图,您正在丢失旧视图
- 每次您使用相同的 ID
R.id.radiobtn
查找视图并在布局中再次添加相同的视图时,这是不可能的,因此会出现问题。
for (int i=0;i<drawables;i++){
// 1,2
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//
final View rowView = inflater.inflate(R.layout.radiobutton, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); // same view with same id R.id.radiobtn
rd.setText(current.getAnswers().get(i).getAns());
}
解决方案:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
// create a radio group as container with direction
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
// create radio button, with id and text
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
// add radio button into group
l1.addView(rd);
}
// finally, add radio group with buttons into parent layout
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
你已经很接近了,我想只要改变一些线路就可以了。
public void drawRAnswers(int pst){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
for (int i=0;i<drawables;i++){
final View rowView = inflater.inflate(R.layout.radiobutton, null);
// Add the new row before the add field button.
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
rd.setText(current.getAnswers().get(i).getAns());
rd.setId(i);
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
//or
parentLinearLayout.addView(l1);
}
RadioGroups
是幕后美化的 LinearLayouts,这意味着它们可以支持 any children,但只会处理 RadioButtons
如果他们是 组 的直接 children,则为相同的“组”。例如:
想象一下这个层次结构:
<RadioGroup> // THIS YOU HAVE IN XML
<RadioButton 1> // ALL THESE YOU CREATE PROGRAMMATICALLY
<RadioButton 2>
<RadioButton 3>
</RadioGroup>
所以你的 XML 看起来像:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/your_radio_button"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Radio Buttons will be programmatically added here -->
</RadioGroup>
所以……您以编程方式创建所有内容,这很好。你有一个“循环”,比如……
final RadioGroup group = //find your_radio_button in the layout.
// now iterate
for (Something something : listOfSomethings) {
final RadioButton rb = new RadioButton(context);
rb.setId(View.generateViewId());
rb.setText(something.giveMeTheText());
rb.setLayoutParams(layoutParams); // make these depending what your container for the RadioGroup is.
rb.setOnCheckedChangeListener(your OnCheckedChangeListener);
group.add(rb);
}
如果你需要监听变化,显然添加一个普通的CompoundButton.OnCheckedChangeListener
并覆盖:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do what you need ;)
}
请记住,如果按钮有 onCheckedChangeListener
,那么在您执行 group.add(rb)
的那一刻,它将被触发……如果您将其设置为已选中(我认为即使您不这样做,它也会触发't with a “false”),所以你可能想延迟监听器的分配,直到你完成向 RadioGroup 添加按钮,然后迭代它们并同时添加它们(这不会触发回调就在那里,因为按钮已经在层次结构中并且没有改变)。
如果你需要插入一些东西 other 而不是 RadioButton 作为你 RadioGroup
的直接 child 那是另一个问题的主题(这是可行的,不是自动的,需要你做一些 extra-work)。