Radiogroup implements multi row arrangement of radio boxes

The use of radiogroup is very simple, but generally, it can only be arranged horizontally or vertically. If it is arranged horizontally, it is not so simple.

Maybe it's time for children's shoes. Will it be OK to write the RadioButton into the linelayout soon? After verification, I can do that. I did the same at the beginning. However, a bug was found in the operation - the radio button is no longer a radio button. Moreover, the selection event will not be monitored. This requires us to find a way. In fact, it is not difficult to implement. Just use a few more radiogroups (deal with some events in the code).

Upper Code:

1. Layout in XML:

<RelativeLayout
  android:id="@+id/main_tab_container"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="30dp">

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

   <RadioButton
    android:id="@+id/rb_1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="GBP英镑" />

   <RadioButton
    android:id="@+id/rb_2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="HKD港元" />

   <RadioButton
    android:id="@+id/rb_3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="USD美元Ԫ" />
  </RadioGroup>

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

   <RadioButton
    android:id="@+id/rb_4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="CHF瑞士法郎" />

   <RadioButton
    android:id="@+id/rb_5"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="SGD新加坡元" />

   <RadioButton
    android:id="@+id/rb_6"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="SEK瑞典克朗" />
  </RadioGroup>

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

   <RadioButton
    android:id="@+id/rb_7"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="JPY日元" />

   <RadioButton
    android:id="@+id/rb_8"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="CAD加拿大元Ԫ" />

   <RadioButton
    android:id="@+id/rb_9"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="@dimen/RB_text_size"
    android:text="AUD澳大利亚元" />
  </RadioGroup>

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

   <RadioButton
    android:id="@+id/rb_10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/RB_text_size"
    android:text="EOR欧元Ԫ" />

  </RadioGroup>

</RelativeLayout>

In this way, the multi line layout is realized. This is only a part of my layout, where Android: textsize = "@ dimension / RB"_ text_ Size "defines the font size for yourself

2. Use and handling in activity:

public class SelectMoneyActivity extends BaseActivity {

 String strBtnSelected = ""; //记录选择的是哪个选项

 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) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_select_money);

  initView();

 }

 private void initView() {

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

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

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

//创建监听器,为每个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);

//点击事件的监听器
 public class BtnSelected implements View.OnClickListener {

  private String btnId;

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

  @Override
  public void onClick(View v) {
   strBtnSelected = btnId;  //选择的某一项
   isSelect = true;
   //点击了第一行 ,就把另外行的点击项清空
   if (btnId.equals("1") || btnId.equals("2") || btnId.equals("3")) {

    rg2.clearCheck();
    rg3.clearCheck();
    rg4.clearCheck();
   } else if (btnId.equals("4") || btnId.equals("5") || btnId.equals("6")) {
    rg1.clearCheck();
    rg3.clearCheck();
    rg4.clearCheck();
   } else if (btnId.equals("7") || btnId.equals("8") || btnId.equals("9")) {
    rg1.clearCheck();
    rg2.clearCheck();
    rg4.clearCheck();
   } else {
    rg1.clearCheck();
    rg2.clearCheck();
    rg3.clearCheck();
   }
  }
 }
}

It's done. Another way is to customize the implementation of radiogroup, but it's a little complicated. I still went home from work

Supplement:

Use radiogroup.setcheck (ID of RadioButton) to initialize the a button selected by default, but listen for problems that will not be executed

Solution: check = "true" has been set in the layout for button a; Removing this attribute will perform listening

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>