SoFunction
Updated on 2025-04-12

RadioGroup implements multi-row arrangement of radio boxes

RadioGroup is very simple to use, but in general, it can only be arranged horizontally or vertically. It wouldn't be that simple if you arrange the multi-horizontal ones.

Maybe there is a child's shoes that should be said. Will it be possible to write RadioButton into LineLayout soon? After testing, I can do that, and at the beginning I did the same. However, I found a bug when I ran it - the radio button is no longer a single choice. And the selection event will not be heard. This requires us to find a way. In fact, it is not difficult to achieve. Just use a few more RadioGroups (to handle some events in the code).

On code:

Layout in:

<RelativeLayout
  android:
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="30dp">

  <RadioGroup
   android:
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_margin="5dp"
   android:orientation="horizontal">

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="GBP pound" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="HKD * Dollar" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="USD USD" />
  </RadioGroup>

  <RadioGroup
   android:
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_below="@+id/radio1"
   android:layout_margin="5dp"
   android:orientation="horizontal">

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="CHF Swiss Franc" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="SGD Singapore Dollar" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="SEK Swedish Krona" />
  </RadioGroup>

  <RadioGroup
   android:
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_below="@+id/radio2"
   android:layout_margin="5dp"
   android:orientation="horizontal">

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="JPY yen" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="CAD Canadian Yuan" />

   <RadioButton
    android:
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="AUD Australian Dollar" />
  </RadioGroup>

  <RadioGroup
   android:
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_below="@+id/radio3"
   android:layout_margin="5dp"
   android:orientation="horizontal">

   <RadioButton
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/RB_text_size"
    android:text="EOR Euro" />

  </RadioGroup>

</RelativeLayout>

This implements a multi-line layout, which is only part of my layout, where android:textSize=”@dimen/RB_text_size” defines the font size for myself.

Use and processing in:

public class SelectMoneyActivity extends BaseActivity {


 String strBtnSelected = ""; //Record which option is selected
 private RadioGroup rg1, rg2, rg3, rg4;
 private RadioButton rb_1, rb_2, rb_3, rb_4, rb_5, rb_6, rb_7, rb_8, rb_9, rb_10;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_select_money);

  initView();

 }


 private void initView() {

  rg1 = (RadioGroup) findViewById(.radio1);
  rg2 = (RadioGroup) findViewById(.radio2);
  rg3 = (RadioGroup) findViewById(.radio3);
  rg4 = (RadioGroup) findViewById(.radio4);

  rb_1 = (RadioButton) findViewById(.rb_1);
  rb_2 = (RadioButton) findViewById(.rb_2);
  rb_3 = (RadioButton) findViewById(.rb_3);
  rb_4 = (RadioButton) findViewById(.rb_4);
  rb_5 = (RadioButton) findViewById(.rb_5);
  rb_6 = (RadioButton) findViewById(.rb_6);
  rb_7 = (RadioButton) findViewById(.rb_7);
  rb_8 = (RadioButton) findViewById(.rb_8);
  rb_9 = (RadioButton) findViewById(.rb_9);
  rb_10 = (RadioButton) findViewById(.rb_10);

  btn_back = (Button) findViewById(.btn_back);
  btn_next = (Button) findViewById(.btn_next);

//Create a listener and register a listener for each RadioButton
  BtnSelected btnSelected1 = new BtnSelected("1");
  BtnSelected btnSelected2 = new BtnSelected("2");
  BtnSelected btnSelected3 = new BtnSelected("3");
  BtnSelected btnSelected4 = new BtnSelected("4");
  BtnSelected btnSelected5 = new BtnSelected("5");
  BtnSelected btnSelected6 = new BtnSelected("6");
  BtnSelected btnSelected7 = new BtnSelected("7");
  BtnSelected btnSelected8 = new BtnSelected("8");
  BtnSelected btnSelected9 = new BtnSelected("9");
  BtnSelected btnSelected10 = new BtnSelected("10");

  rb_1.setOnClickListener(btnSelected1);
  rb_2.setOnClickListener(btnSelected2);
  rb_3.setOnClickListener(btnSelected3);
  rb_4.setOnClickListener(btnSelected4);
  rb_5.setOnClickListener(btnSelected5);
  rb_6.setOnClickListener(btnSelected6);
  rb_7.setOnClickListener(btnSelected7);
  rb_8.setOnClickListener(btnSelected8);
  rb_9.setOnClickListener(btnSelected9);
  rb_10.setOnClickListener(btnSelected10);


//The listener of the click event public class BtnSelected implements  {

  private String btnId;

  public BtnSelected(String str) {
   btnId = str;
  }

  @Override
  public void onClick(View v) {
   strBtnSelected = btnId;//A selected item   isSelect = true;
   //Click on the first line and clear the click items of the other line   if (("1") || ("2") || ("3")) {

    ();
    ();
    ();
   } else if (("4") || ("5") || ("6")) {
    ();
    ();
    ();
   } else if (("7") || ("8") || ("9")) {
    ();
    ();
    ();
   } else {
    ();
    ();
    ();
   }
  }
 }
}

It's done. Another way is to customize the RadioGroup implementation, but this is a bit complicated. I still came home from get off work.

Replenish:

Use (RadioButton's id) to initialize button A by default, but listening will not execute

solve:Because check=”true” has been set to button A in the layout; removing this property will perform monitoring.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.