0

我想添加单选按钮,以便在 4 个按钮中一次只选择一个,我想将它们放置为:

        RadioButton1  RadioButton2
        RadioButton3  RadioButton4

我正在尝试以下代码,但 1&2 形成一个 grp,3&4 形成一个不同的 grp,并且一次选择了两个值。任何人都可以检查它并分享正确的方法吗?

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    >

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

    <RadioGroup 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rdogrp1"
        android:orientation="horizontal"            
        >

    <RadioButton 
        android:id="@+id/RadioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Orange"
        android:textColor="#000000"
        />

    <RadioButton 
        android:id="@+id/RadioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="BlueBerry"
        android:textColor="#000000"
        />
    </RadioGroup>

    <RadioGroup 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rdogrp2"
        android:orientation="horizontal"            
        >
    <RadioButton 
        android:id="@+id/RadioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple  "
        android:textColor="#000000"
        />

    <RadioButton 
        android:id="@+id/RadioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="Santra :P"
        android:textColor="#000000"
        />
    </RadioGroup>

    </RadioGroup>
</LinearLayout>
4

3 回答 3

0

好的,我为您找到了解决方案:

我知道这不是最好的解决方案,但它有效!:)

只需复制下面的 xml 文件和活动代码:

活动:

package com.example.stackoverflow;

import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    private static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
        final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
        final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
        final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);

        radiobutton1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton1.setChecked(true);
                radiobutton2.setChecked(false);
                radiobutton3.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton2.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton3.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton3.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton2.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton4.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton4.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton2.setChecked(false);
                radiobutton3.setChecked(false);
                return true;
            }
        });

    }

}

活动主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#FFDDDDDD" >

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

            <RadioGroup
                android:id="@+id/rdogrp1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/RadioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Orange"
                    android:textColor="#000000" />

                <RadioButton
                    android:id="@+id/RadioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:text="BlueBerry"
                    android:textColor="#000000" />
            </RadioGroup>
        </TableRow>

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

            <RadioGroup
                android:id="@+id/rdogrp2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/RadioButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Apple  "
                    android:textColor="#000000" />

                <RadioButton
                    android:id="@+id/RadioButton4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:text="Santra :P"
                    android:textColor="#000000" />
            </RadioGroup>
        </TableRow>
    </TableLayout>


</RelativeLayout>
于 2013-05-13T14:32:34.697 回答
0

试试这样..

         <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<RadioGroup
    android:id="@+id/rdogrp_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/RadioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.20"
        android:text="India"
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/RadioButton2"
        android:layout_width="74dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.57"
        android:text="Austraila"
        android:textColor="#000000" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/RadioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="Srilanka  "
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/RadioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.48"
        android:text="SouthAfrica"
        android:textColor="#000000" />
</LinearLayout>

 </RadioGroup>

 </LinearLayout>

如果您解决了问题,请告诉我...

于 2013-05-13T12:31:27.680 回答
0

为此,您需要创建only one RadioGroup并添加四个不同的 RadioButton。对于添加 UI,请参阅此 url:Manage Layout Inside a Horizo​​ntal Oriented Radiogroup

我希望这能帮到您。

于 2013-05-13T12:15:30.303 回答