我想添加单选按钮,以便在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>
答案 0 :(得分:0)
为此你需要创建only one RadioGroup
并添加四个不同的RadioButton。
要添加UI,请参阅此网址:Manage Layout Inside a Horizontal Oriented Radiogroup。
我希望这会对你有所帮助。
答案 1 :(得分: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>
如果您解决了问题,请告诉我......
答案 2 :(得分: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;
}
});
}
}
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>