改变无线电按钮的位置 [英] Changing the position of radio button

查看:143
本文介绍了改变无线电按钮的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以改变的单选按钮位置从左至右。我指的是绿色选择的按钮将在右边,在它的左边的文字。可能吗? (默认键左,右文)

Can I change the radio button position from left to right. I mean the green selected button will be on the right and the text on its left. Is it possible? (by default button left, text right)

推荐答案

单选按钮不灵活,你(或几乎任何人)想要的。你可以建立自己的自定义窗口小部件,如果您不熟悉这样做可以是艰巨的。或者你可以做我喜欢的解决方法。

RadioButtons are not as flexible as you (or nearly anyone) wants. You can build your own custom widget, which can be daunting if you are unfamiliar with doing so. Or you can do my favorite work-around.

处理单选按钮,好像他们是普通按钮 - 不要使用RadioGroup中的功能。现在的的具有手动控制的检查。通过消除RadioGroup中,你是自由的,反正你要创建的布局。

Handle RadioButtons as if they were regular buttons--do not use the RadioGroup functionality. Now you have to control the check manually. By eliminating the RadioGroup, you are free to create your layouts anyway you want.

下面是一个简单的XML布局,它使用单选按钮在TableLayout有文字到左边和图像的每个按钮的右边:

Here is a sample xml layout which uses RadioButtons in a TableLayout that has text to the left and an image to the right of each button:

<?xml version="1.0" encoding="utf-8"?>

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

    <TableRow
        android:id="@+id/row_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 1" />

        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pretty_pic_1" />
    </TableRow>

    <TableRow
        android:id="@+id/row_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 2" />

        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pretty_pic_3" />
    </TableRow>

    <TableRow
        android:id="@+id/row_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 3" />

        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pretty_pic_3" />
    </TableRow>

</TableLayout>

但你还没有完成。现在,您必须手动处理您的单选按钮。这里是我喜欢做的事是:

But you're not done yet. You must now handle your radio buttons manually. Here is how I like to do it:

class FooActivity extends Activity {
RadioButton m_rb1, m_rb2;
TableRow m_row1, m_row2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    m_rb1 = (RadioButton) findViewById(R.id.rb1);
    m_rb2 = (RadioButton) findViewById(R.id.rb2);

    m_row1 = (TableRow) findViewById(R.id.row_1);
    m_row1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            m_rb1.setChecked(true);
            m_rb2.setChecked(false);            
        }
    });

    m_row2 = (TableRow) findViewById(R.id.row_2);
    m_row2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            m_rb1.setChecked(false);            
            m_rb2.setChecked(true);
        }
    });

}

}

请注意,我希望用户通过选择文本,图像或按钮本身来选择单选按钮。所以,我提出了整个TableRows可点击的对象。

Note that I want the user to select the RadioButton by choosing the text, the picture, or the button itself. So I made the entire TableRows the clickable object.

希望这有助于!

这篇关于改变无线电按钮的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆