首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何对来自不同LinearLayouts的RadioButton进行分组?

如何对来自不同LinearLayouts的RadioButton进行分组?
EN

Stack Overflow用户
提问于 2012-05-05 18:34:18
回答 21查看 47.1K关注 0票数 101

我想知道是不是可以把每一个人都分成一组

在一个独特的

保持相同的结构。我的结构看起来像这样:

LinearLayout

_

main

LinearLayout

_

1

RadioButton1

代码语言:javascript
复制
- LinearLayout\_2 
    - RadioButton2

代码语言:javascript
复制
- LinearLayout\_3 
    - RadioButton3

如你所见,现在每个

是不同的

..。我尝试使用下面的结构,但它不起作用:

辐射群

LinearLayout

_

main

LinearLayout

_

1

RadioButton1

代码语言:javascript
复制
- LinearLayout\_2 
        - RadioButton2

代码语言:javascript
复制
- LinearLayout\_3 
        - RadioButton3

EN

回答 21

Stack Overflow用户

发布于 2012-11-08 00:25:12

谷歌/安卓的好员工似乎认为,当你使用RadioButtons时,你不需要安卓UI/布局系统的其他所有方面的灵活性。简单地说:他们不想让你嵌套布局和单选按钮。叹息。

所以你必须解决这个问题。这意味着你必须自己实现单选按钮。

这真的不是太难。在您的onCreate()中,使用它们自己的onClick()设置您的RadioButtons,这样当它们被激活时,它们就会setChecked(真),并对其他按钮执行相反的操作。例如:

代码语言:javascript
复制
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() 

}

是啊,我知道--很老套。但它是有效的。祝你好运!

票数 55
EN

Stack Overflow用户

发布于 2013-01-14 07:04:36

使用我创建的这个类。它将在您的层次结构中找到所有可检查的子项。

代码语言:javascript
复制
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));
    }
}
}
票数 29
EN

Stack Overflow用户

发布于 2013-04-18 06:35:38

我写了这个简单的类。

就像这样使用它:

代码语言:javascript
复制
// add any number of RadioButton resource IDs here
GRadioGroup gr = new GRadioGroup(this, 
    R.id.radioButton1, R.id.radioButton2, R.id.radioButton3);

或者

代码语言:javascript
复制
GRadioGroup gr = new GRadioGroup(rb1, rb2, rb3);
// where RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
// etc.

例如,您可以在Activity的onCreate()中调用它。不管是哪一个

单击后,其他选项将变为未选中状态。另外,无所谓,如果一些

是在一些

,或者不是。

下面是这个类:

代码语言:javascript
复制
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);
            }

        }
    };

}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10461005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档