<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/public_line" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/Public" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:text="Public" android:layout_marginLeft="16dp" android:layout_marginTop="24dp" android:textSize="20dp" android:textColor="@android:color/black"/> <RadioButton android:id="@+id/public_button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="16dp" android:layout_marginTop="24dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginBottom="8dp" android:text="league is visible to the public, that is, anyone can join your league" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/Private" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:text="Private" android:layout_marginLeft="16dp" android:layout_marginTop="24dp" android:textSize="20dp" android:textColor="@android:color/black"/> <RadioButton android:id="@+id/private_button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="16dp" android:layout_marginTop="24dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginBottom="8dp" android:text="Only people with invite code can join" /> </RadioGroup> <TextView android:layout_alignParentBottom="true" android:background="@drawable/orange_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Done" android:padding="16dp" android:textSize="20dp" android:textColor="@android:color/black" android:layout_weight="1" android:gravity="center"/> </RelativeLayout> Here is my layout. It allows both the radio buttons to be selected. Can someone help me figure out what the problem is? From what I know the problem is that the radio buttons are not directly in the radio group, but how am I supposed to accommodate them in the radio group directly to get this same layout. Else is there an alternate way to solve the problem
4 Réponses :
Le radiobutton code> s doit être des enfants directs de
radiogroupe code> pour le faire gérer automatiquement la sélection unique.
De votre présentation, il semble que vous essayez d'afficher du texte sur le côté de votre radiobutton code> s. Pour ce faire, vous pouvez simplement définir l'attribut
android: texte code> sur le radiobutton code>, pas besoin d'utiliser supplémentaire
textview code> s. P>
Vos radiobuttons doivent être un enfant de radiogroupe exaclly comme celui-ci mais dans votre cas est enfant de linearlayout et chaque bouton radio peut être sélectionné P> P>
Essayez simplement celui-ci strong>
Il existe deux façons d'atteindre vos solutions
Tout d'abord est que les radiobuttons définissent directement les enfants de radiogroupe comme ci-dessous P>
publicButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ publicButton.setChecked(true); privateButton.setChecked(false); } } }); privateButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ publicButton.setChecked(false); privateButton.setChecked(true); } } });
Cela semble dupliquer. Consultez cette page Stackoverflow.com/a/18179176/4295522
@Acuthgovind Vous voulez dire de dire tout en conservant le même design?
Vous voudrez peut-être écrire une logique personnalisée dans ce cas.