在Android中添加单选按钮

在Android中添加单选按钮,android,android-layout,radio-button,Android,Android Layout,Radio Button,我希望添加单选按钮,以便在4个按钮中一次只选择其中一个,并将其放置为: RadioButton1 RadioButton2 RadioButton3 RadioButton4 我正在尝试下面的代码,但是1和2形成一个grp,3和4形成一个不同的grp,一次选择两个值。有人能检查它并分享正确的方法吗 <ImageView android:id="@+id/imageView1" android:layout_width="

我希望添加单选按钮,以便在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>

为此,您只需创建一个RadioGroup并添加四个不同的RadioButton。 要添加UI,请参考以下url:

我希望这会对你有所帮助。

像这样试试

         <?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>


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

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

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

只需复制以下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;
            }
        });

    }

}
activity_main.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>

谢谢您的时间,但它仍然会选择所有单选按钮,而不管它只有一个grp。