我想知道是不是可以把每一个人都分成一组
在一个独特的
保持相同的结构。我的结构看起来像这样:
LinearLayout
_
main
LinearLayout
_
1
RadioButton1
- LinearLayout\_2
- RadioButton2
- LinearLayout\_3
- RadioButton3
如你所见,现在每个
是不同的
..。我尝试使用下面的结构,但它不起作用:
辐射群
LinearLayout
_
main
LinearLayout
_
1
RadioButton1
- LinearLayout\_2
- RadioButton2
- LinearLayout\_3
- RadioButton3
发布于 2012-11-08 00:25:12
谷歌/安卓的好员工似乎认为,当你使用RadioButtons时,你不需要安卓UI/布局系统的其他所有方面的灵活性。简单地说:他们不想让你嵌套布局和单选按钮。叹息。
所以你必须解决这个问题。这意味着你必须自己实现单选按钮。
这真的不是太难。在您的onCreate()中,使用它们自己的onClick()设置您的RadioButtons,这样当它们被激活时,它们就会setChecked(真),并对其他按钮执行相反的操作。例如:
class FooActivity {
RadioButton m_one, m_two, m_three;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
m_one = (RadioButton) findViewById(R.id.first_radio_button);
m_two = (RadioButton) findViewById(R.id.second_radio_button);
m_three = (RadioButton) findViewById(R.id.third_radio_button);
m_one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
m_one.setChecked(true);
m_two.setChecked(false);
m_three.setChecked(false);
}
});
m_two.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
m_one.setChecked(false);
m_two.setChecked(true);
m_three.setChecked(false);
}
});
m_three.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
m_one.setChecked(false);
m_two.setChecked(false);
m_three.setChecked(true);
}
});
...
} // onCreate()
}
是啊,我知道--很老套。但它是有效的。祝你好运!
发布于 2013-01-14 07:04:36
使用我创建的这个类。它将在您的层次结构中找到所有可检查的子项。
import java.util.ArrayList;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;
public class MyRadioGroup extends LinearLayout {
private ArrayList mCheckables = new ArrayList();
public MyRadioGroup(Context context) {
super(context);
}
public MyRadioGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRadioGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
parseChild(child);
}
public void parseChild(final View child)
{
if(child instanceof Checkable)
{
mCheckables.add(child);
child.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < mCheckables.size();i++)
{
Checkable view = (Checkable) mCheckables.get(i);
if(view == v)
{
((Checkable)view).setChecked(true);
}
else
{
((Checkable)view).setChecked(false);
}
}
}
});
}
else if(child instanceof ViewGroup)
{
parseChildren((ViewGroup)child);
}
}
public void parseChildren(final ViewGroup child)
{
for (int i = 0; i < child.getChildCount();i++)
{
parseChild(child.getChildAt(i));
}
}
}
发布于 2013-04-18 06:35:38
我写了这个简单的类。
就像这样使用它:
// add any number of RadioButton resource IDs here
GRadioGroup gr = new GRadioGroup(this,
R.id.radioButton1, R.id.radioButton2, R.id.radioButton3);
或者
GRadioGroup gr = new GRadioGroup(rb1, rb2, rb3);
// where RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
// etc.
例如,您可以在Activity的onCreate()中调用它。不管是哪一个
单击后,其他选项将变为未选中状态。另外,无所谓,如果一些
是在一些
,或者不是。
下面是这个类:
package pl.infografnet.GClasses;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewParent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class GRadioGroup {
List radios = new ArrayList();
/**
* Constructor, which allows you to pass number of RadioButton instances,
* making a group.
*
* @param radios
* One RadioButton or more.
*/
public GRadioGroup(RadioButton... radios) {
super();
for (RadioButton rb : radios) {
this.radios.add(rb);
rb.setOnClickListener(onClick);
}
}
/**
* Constructor, which allows you to pass number of RadioButtons
* represented by resource IDs, making a group.
*
* @param activity
* Current View (or Activity) to which those RadioButtons
* belong.
* @param radiosIDs
* One RadioButton or more.
*/
public GRadioGroup(View activity, int... radiosIDs) {
super();
for (int radioButtonID : radiosIDs) {
RadioButton rb = (RadioButton)activity.findViewById(radioButtonID);
if (rb != null) {
this.radios.add(rb);
rb.setOnClickListener(onClick);
}
}
}
/**
* This occurs everytime when one of RadioButtons is clicked,
* and deselects all others in the group.
*/
OnClickListener onClick = new OnClickListener() {
@Override
public void onClick(View v) {
// let's deselect all radios in group
for (RadioButton rb : radios) {
ViewParent p = rb.getParent();
if (p.getClass().equals(RadioGroup.class)) {
// if RadioButton belongs to RadioGroup,
// then deselect all radios in it
RadioGroup rg = (RadioGroup) p;
rg.clearCheck();
} else {
// if RadioButton DOES NOT belong to RadioGroup,
// just deselect it
rb.setChecked(false);
}
}
// now let's select currently clicked RadioButton
if (v.getClass().equals(RadioButton.class)) {
RadioButton rb = (RadioButton) v;
rb.setChecked(true);
}
}
};
}
https://stackoverflow.com/questions/10461005
复制相似问题