Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何将单选组添加到表内的单选按钮?_Xml_Android - Fatal编程技术网

Xml 如何将单选组添加到表内的单选按钮?

Xml 如何将单选组添加到表内的单选按钮?,xml,android,Xml,Android,我有多个单选按钮,我想使用一个表来布局,但也包括在一个单选组中。我有以下xml布局: <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/Group1"> <TableLayout android:id="@+id/Ra

我有多个单选按钮,我想使用一个表来布局,但也包括在一个单选组中。我有以下xml布局:

<RadioGroup android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"
      android:id="@+id/Group1">

    <TableLayout android:id="@+id/RadioButtons" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

        <TableRow>
            <RadioButton android:id="@+id/rad1" 
                android:text="RButton1" 
                android:layout_width="105px" 
                android:layout_height="wrap_content" 
                android:textSize="13px"></RadioButton>
            <RadioButton android:id="@+id/rad2" 
                android:text="RButton2" 
                android:layout_width="105px" 
                android:textSize="13px" 
                android:layout_height="wrap_content"></RadioButton>
            <RadioButton android:id="@+id/rad3" 
                android:text="RButton3" 
                android:layout_width="105px" 
                android:textSize="13px" 
                android:layout_height="wrap_content"></RadioButton>
        </TableRow>
      </TableLayout>
</RadioGroup>  


但不幸的是,表中的单选按钮似乎忽略了一个事实,即它们位于RadioGroup标记中,因此您可以同时选择多个单选按钮。我注意到,通过移除桌子,只需使用单选按钮,它就可以正常工作。我怎样才能克服这个问题?这会不会像在桌子里面而不是外面声明无线电组那样简单?谢谢你的帮助

您的
RadioButton
小部件必须是
RadioGroup
的直接子部件,才能使group effect生效。

这是我的RadioGroup/RadioButton扩展(SoftRadioGroup/SoftRadioButton)。
rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
        rg2 = (RadioGroup)findViewById(R.id.radioGroup2);
        rg1.setOnCheckedChangeListener(this);
        rg2.setOnCheckedChangeListener(this);
    }
    boolean rg1b = false;
    boolean rg2b = false;

    @Override
    public void onCheckedChanged(RadioGroup rgId, int radioButtonId) {
        switch (rgId.getId()) {
        case R.id.radioGroup1:
            rg1b=true;
            if(rg2b)
                rg2.clearCheck();
            break;

        case R.id.radioGroup2:
            rg1b=true;
            if(rg1b)
                rg1.clearCheck();
            break;
        }
布局XML中不再需要RadioGroup。您可以使用名为group的属性对单选按钮进行分组

软无线电按钮:

import java.util.HashMap;
import java.util.Random;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;

public class SoftRadioButton extends RadioButton {

    private static HashMap<String, SoftRadioGroup> GROUP_MAPPINGS = new HashMap<String, SoftRadioGroup>();
    private String mGroupName;

    public SoftRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        addToGroup(attrs);
    }

    public SoftRadioGroup getRadioGroup() {
        return GROUP_MAPPINGS.get(mGroupName);
    }

    private void addToGroup(AttributeSet attrs) {
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (attrs.getAttributeName(i).equals("group")) {
                String groupName = attrs.getAttributeValue(i);
                SoftRadioGroup group;
                if ((group = GROUP_MAPPINGS.get(groupName)) != null) {
                    // RadioGroup already exists
                    group.addView(this);
                    setOnClickListener(group);
                    mGroupName = groupName;

                } else {
                    // this is the first RadioButton in the RadioGroup
                    group = new SoftRadioGroup();
                    group.addView(this);
                    mGroupName = groupName;
                    setOnClickListener(group);

                    GROUP_MAPPINGS.put(groupName, group);
                }
                return;
            }
        }
        // group is not specified in the layout xml. Let's generate a random
        // RadioGroup
        SoftRadioGroup group = new SoftRadioGroup();
        group.addView(this);
        Random rn = new Random();
        String groupName;
        do {
            groupName = Integer.toString(rn.nextInt());
        } while (GROUP_MAPPINGS.containsKey(groupName));
        GROUP_MAPPINGS.put(groupName, group);
        mGroupName = groupName;
        setOnClickListener(group);

    }

}
import java.util.HashMap;
导入java.util.Random;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.util.Log;
导入android.widget.RadioButton;
公共类SoftRadioButton扩展了RadioButton{
私有静态HashMap GROUP_MAPPINGS=new HashMap();
私有字符串名称;
公共SoftRadioButton(上下文、属性集属性){
超级(上下文,attrs);
addToGroup(attrs);
}
公共SoftRadioGroup getRadioGroup(){
返回组\u MAPPINGS.get(mGroupName);
}
私有void addToGroup(属性集属性){
对于(int i=0;i
软件组:

import java.util.ArrayList;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class SoftRadioGroup implements OnClickListener {

    private ArrayList<RadioButton> buttons = new ArrayList<RadioButton>();

    public void addView(RadioButton button) {
        buttons.add(button);
    }

    @Override
    public void onClick(View v) {
        for (RadioButton button : buttons) {
            button.setChecked(false);
        }
        RadioButton button = (RadioButton) v;
        button.setChecked(true);
    }

    public RadioButton getCheckedRadioButton() {
        for (RadioButton button : buttons) {
            if (button.isSelected())
                return button;
        }
        return null;
    }

    public int getChildCount() {
        return buttons.size();
    }

    public RadioButton getChildAt(int i) {
        return buttons.get(i);
    }

    public void check(SoftRadioButton button) {
        if (buttons.contains(button)) {
            for (RadioButton b : buttons) {
                b.setChecked(false);
            }
        }
    }

}
import java.util.ArrayList;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.RadioButton;
公共类SoftRadioGroup实现OnClickListener{
私有ArrayList按钮=新建ArrayList();
公共无效添加视图(单选按钮){
按钮。添加(按钮);
}
@凌驾
公共void onClick(视图v){
用于(单选按钮:按钮){
按钮。setChecked(假);
}
RadioButton按钮=(RadioButton)v;
按钮。setChecked(true);
}
公共单选按钮getCheckedRadioButton(){
用于(单选按钮:按钮){
if(button.isSelected())
返回按钮;
}
返回null;
}
public int getChildCount(){
返回按钮;
}
公共单选按钮getChildAt(int i){
返回按钮。获取(i);
}
公共无效检查(软无线电按钮){
if(按钮。包含(按钮)){
用于(单选按钮b:按钮){
b、 setChecked(假);
}
}
}
}
嵌入在表中的XML布局中的用法(2组,每组2个按钮):



因此,没有办法用表格来构造按钮并使其成为单选组的一部分?前提是
RadioButton
小部件是
RadioGroup
的直接子项。因此,,您的
RadioButton
小部件需要位于表的单个单元格中的行或列中。如果我通过编程将每个单选按钮分配到一个组中会怎么样?@nobalG:
RadioButton
是通过布局还是通过Java代码添加为
RadioGroup
的子项应该无关紧要。请不要只是转储代码作为你的回答。一个解释会很有用。警告:代码将泄漏视图。每次创建视图时,它都会将所有SoftRadioButton视图添加到静态地图中。用自定义视图解决这个问题是个好主意。我认为最好的解决方案是将视图组添加到视图层次结构的根位置,让它找到所有子视图并将它们配对到组中。通过添加attr.xml来修复属性“group”:“内存泄漏的快速修复:添加到SoftRadioButton方法onDetachedFromWindow(),并调用组映射。remove(mGroupName);这非常有用,我添加了一个新的构造函数,将groupname作为字符串,以便能够动态创建这些构造函数。
<TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="1" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <Your.Package.SoftRadioButton
                    android:id="@+id/filterActivity_RadioButton_byDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:contentDescription="date"
                    android:text="@string/filterActivity_RadioButton_byDate"
                    fake:group="orderBy" />

                <Your.Package.SoftRadioButton
                    android:id="@+id/filterActivity_RadioButton_byPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="price"
                    android:text="@string/filterActivity_RadioButton_byPrice"
                    fake:group="orderBy" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <Your.Package.SoftRadioButton
                    android:id="@+id/filterActivity_RadioButton_asc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="down"
                    android:text="@string/filterActivity_RadioButton_asc"
                    fake:group="direction" />

                <Your.Package.SoftRadioButton
                    android:id="@+id/filterActivity_RadioButton_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:contentDescription="up"
                    android:text="@string/filterActivity_RadioButton_desc"
                    fake:group="direction" />
            </TableRow>
        </TableLayout>