Java 在tablelayout Android中排列单选按钮

Java 在tablelayout Android中排列单选按钮,java,android,radio-button,Java,Android,Radio Button,我想在多行和多列的表格布局中排列单选按钮,控件是使用下面的代码排列的,但不能在同一个单选按钮组中关联 你能帮忙吗,下面是我试过的代码 <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioGroup android:id="@+id/radioLang1"

我想在多行和多列的表格布局中排列单选按钮,控件是使用下面的代码排列的,但不能在同一个单选按钮组中关联

你能帮忙吗,下面是我试过的代码

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/radioLang1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:dividerPadding="0dp"
            android:orientation="vertical" >

            <TableRow>

                <RadioButton
                    android:id="@+id/radioEnglish1"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checked="true"
                    android:onClick="onRadioButtonClicked"
                    android:text="@string/lang1"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radioTransliteration"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checked="true"
                    android:onClick="onRadioButtonClicked"
                    android:text="Punjabi Transliteration"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />
            </TableRow>

            <TableRow>

                <RadioButton
                    android:id="@+id/radioPunjabi1"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:onClick="onRadioButtonClicked"
                    android:text="Punjabi Vyakhya(Meaning)"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radioHindi"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:onClick="onRadioButtonClicked"
                    android:text="Hindi Mein Padhiye"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />
            </TableRow>

            <RadioButton
                android:id="@+id/radioEnglishTranslation1"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:onClick="onRadioButtonClicked"
                android:text="@string/eng_translate"
                android:textColor="@android:color/black"
                android:textSize="12sp" />
        </RadioGroup>
    </TableLayout>

谢谢


Amandeep

我也有同样的问题,但据我所知,不可能像你所尝试的那样去做

我搜索了一下,找到了一个可能的解决办法

  • 子类表布局()
在另一个线程中,我发现了以下解释:

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

另一种可能的方法是使用类似于TableLayout子类解决方案的方法,为每个单选按钮实现click Listerns,然后控制选中状态。(或者您可以为此创建一个manager类,依我看,这并不难)

另一种方法,如果你没有很多单选按钮,就是做这样的事情(这不是一个很好的解决方案,但它很有效,而且我已经做到了,这很好)。在我的例子中,我必须使用单选按钮,每个单选按钮都在一个表格行中。我为CheckedChanged设置了相同的侦听器,当其中一个被选中时,我取消选中其他侦听器(在本例中,它只是一个)


或者,如果您有时间更改布局,请使用RelativeLayout。不幸的是,我在完成整个布局后发现了这个问题。改变并不难,但对于我所需要的,上面的“解决方法”是有效的,对于这个项目,这样做是可以的。

创建一个RadioGroup类和RadioButton类,然后应用到xml将是最简单的方法


看这个

我回答了你同样问题的更一般的版本

要点是将单选按钮视为常规按钮,并自行处理切换。它不像设计良好的单选按钮系统那样优雅,但我认为它比制作定制小部件更简单。我们必须用我们所拥有的来工作

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() 
}

你可能想看看下面的链接:非常感谢。为了实现这一目标,我从早上开始就绞尽脑汁。它就像一个符咒:)
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() 
}