Несколько строк Радио кнопки в Android?
У меня возникла проблема с наличием radioButtons в нескольких рядах
это мой xml
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_one0Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="5%"
android:id="@+id/radio_one5Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="10%"
android:textSize="13sp"
android:layout_weight="1"
android:id="@+id/radio_one10Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="20%"
android:layout_weight="1"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:id="@+id/radio_one20Id"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="35%"
android:id="@+id/radio_one35Id"
android:textSize="13sp"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="50%"
android:textSize="13sp"
android:id="@+id/radio_one50Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
</RadioGroup>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="65%"
android:textSize="13sp"
android:id="@+id/radio_one65Id"
android:onClick="oneRadioButtonClicked"
android:layout_weight="1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="75%"
android:textSize="13sp"
android:layout_weight="1"
android:id="@+id/radio_one75Id"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="85%"
android:textSize="13sp"
android:id="@+id/radio_one85Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:text="95%"
android:id="@+id/radio_one95Id"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="100%"
android:id="@+id/radio_one100Id"
android:textSize="13sp"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
/>
</RadioGroup>
</RadioGroup>
это код
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id:
if (checked)
one = "0";
break;
case R.id.radio_one5Id:
if (checked)
one = "5";
break;
case R.id.radio_one10Id:
if (checked)
one = "10";
break;
case R.id.radio_one20Id:
if (checked)
one = "20";
break;
case R.id.radio_one35Id:
if (checked)
one = "35";
break;
case R.id.radio_one50Id:
if (checked)
one = "50";
break;
case R.id.radio_one65Id:
if (checked)
one = "65";
break;
case R.id.radio_one75Id:
if (checked)
one = "75";
break;
case R.id.radio_one85Id:
if (checked)
one = "85";
break;
case R.id.radio_one95Id:
if (checked)
one = "95";
break;
case R.id.radio_one100Id:
if (checked)
one = "100";
break;
default:
System.out.println("default");
}
}
это будет выглядеть как ![enter image description here]()
он выберет обе кнопки в 2 строках, я хочу, чтобы она выбирала только одну кнопку в этих строках, спасибо за любую помощь
Ответы
Ответ 1
Поместите одну радиогруппу с вертикальной ориентацией и добавьте два LinearLayouts:
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="@+id/radio_one0Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one5Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="5%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one10Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="10%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one20Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="20%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one35Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="35%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one50Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="50%"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio_one65Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="65%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one75Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="75%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one85Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="85%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one95Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="95%"
android:textSize="13sp" />
<RadioButton
android:id="@+id/radio_one100Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneRadioButtonClicked"
android:text="100%"
android:textSize="13sp" />
</LinearLayout>
</RadioGroup>
Ответ 2
От поиска вокруг, кажется, нет способа сделать это,
Это означает, что вам придется реализовать это поведение макета вручную. Возможны два варианта:
-
Создайте копию RadioGroup, чтобы расширить разный макет или, по крайней мере, позволить вам управлять им динамически.
-
Внедрите свой собственный макет для замены RadioGroup, который расширяет макет по вашему выбору и реализует OnClickListener. Вот хороший пример Как сгруппировать сетку 3x3 переключателей?.
Ответ 3
Это сработало для меня.
Первая строка (NameRadioGroupe2.clearCheck();) очистит другую Radiogroup, а вторая строка добавит галочку в отмеченной кнопке
public void oneRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_one0Id: {
one = "0";
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
break;
}
break;
case R.id.radio_one5Id: {
NameRadioGroupe2.clearCheck();
NameRadioGroupe1.check(view.getId());
one = "5";
break;
}
.
.
.
.
.
case R.id.radio_one65Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "65";
break;
}
case R.id.radio_one75Id: {
NameRadioGroupe1.clearCheck();
NameRadioGroupe2.check(view.getId());
one = "75";
break;
}
.
.
.
.
.
Ответ 4
Один простой способ сделать несколько строк радиокнопки - использовать MultiLineRadioGroup. Он поддерживает как можно больше строк и столбцов.
Использование прост:
В файле build.gradle проекта добавьте:
allprojects {
repositories {
...
maven {
url "https://jitpack.io"
}
...
}
}
В файле приложения или модуля build.gradle добавьте:
dependencies {
...
compile 'com.github.Gavras:MultiLineRadioGroup:v1.0.0.6'
...
}
Вы можете использовать ресурс массива строк в xml для создания своего вида:
В макете XML add:
<com.whygraphics.multilineradiogroup.MultiLineRadioGroup xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_activity_multi_line_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
multi_line_radio_group:default_button="button_2"
multi_line_radio_group:max_in_row="3"
multi_line_radio_group:radio_buttons="@array/radio_buttons" />
и в файле arrays.xml add:
<string-array name="radio_buttons">
<item>button_1</item>
<item>button_2</item>
<item>button_3</item>
<item>button_4</item>
<item>button_5</item>
</string-array>
или добавить их программно:
mMultiLineRadioGroup.addButtons("button to add 1", "button to add 2", "button to add 3");
Ответ 5
Я пытался выработать то же самое.
В итоге я добавил несколько радиогрупп в свои собственные LinearLayouts. При выборе радиокнопки из другой радиогруппы к той, которая выбрана в данный момент, чтобы убедиться, что первые кнопки радиогруппы больше не были выбраны, я добавил .Checked = false для радиокнопок. Функция щелчка. А потом, поскольку вначале у меня возникали ошибки, из-за которых недавно нажатая радиокнопка не проверялась, я добавил .Checked = true к реальной радиокнопке.
Мой XML
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="@style/radios"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rad1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/radios"
android:text="1"
android:checked="true" />
<RadioButton
android:id="@+id/rad2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/radios"
android:text="2" />
<RadioButton
android:id="@+id/rad3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/radios"
android:text="3" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:padding="10dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
style="@style/radios"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rad4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/radios"
android:text="4" />
<RadioButton
android:id="@+id/rad5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/radios"
android:text="5" />
</RadioGroup>
</LinearLayout>
И тогда мой С#
var radio1 = FindViewById<RadioButton>(Resource.Id.rad1);
var radio2 = FindViewById<RadioButton>(Resource.Id.rad2);
var radio3 = FindViewById<RadioButton>(Resource.Id.rad3);
var radio4 = FindViewById<RadioButton>(Resource.Id.rad4);
var radio5 = FindViewById<RadioButton>(Resource.Id.rad5);
radio1.Click += delegate
{
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio1.Checked = true;
};
radio2.Click += delegate
{
radio1.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio2.Checked = true;
};
radio3.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio4.Checked = false;
radio5.Checked = false;
radio3.Checked = true;
};
radio4.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio5.Checked = false;
radio4.Checked = true;
};
radio5.Click += delegate
{
radio1.Checked = false;
radio2.Checked = false;
radio3.Checked = false;
radio4.Checked = false;
radio5.Checked = true;
};
Примитивно, но у меня это сработало.
Ответ 6
Я много исследовал это и наконец нашел решение. Если вы хотите что-то вроде этого:
![enter image description here]()
Сначала вам нужно скачать/создать новый класс, например: link, так как RadioGroup по умолчанию использует LinearLayout. Теперь у вас есть RadioGroup, которая использует RelativeLayout. Осталось только разделить переключатели по процентам (как и в случае с weightSum, просто у вас нет weightSum в RelativeLayout, только LinearLayout) с помощью аккуратного небольшого хака:
<rs.cdl.attendance.UI.RelativeRadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<View
android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<RadioButton
android:id="@+id/start_radio_button"
android:layout_alignRight="@id/strut"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<RadioButton
android:id="@+id/finish_radio_button"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
<RadioButton
android:id="@+id/pause_radio_button"
android:layout_alignRight="@id/strut"
android:layout_alignParentLeft="true"
android:layout_below="@id/start_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<RadioButton
android:id="@+id/continue_radio_button"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:layout_below="@id/finish_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</rs.cdl.attendance.UI.RelativeRadioGroup>
Ответ 7
Я знаю, что этот пост старый, но если кто-то все еще хочет разобраться с этой проблемой, попробуйте эту простую библиотеку: https://github.com/mrHerintsoaHasina/flextools. Вы можете иметь много переключателей, как вам нравится, и обрабатывать различные отображения внутри RadioGroup.