Android radiogroup multi line display effect to solve the problem of single selection

Introduction

As shown in the figure below, this is a navigation selection pop-up box. Make a single selection, and then listen for the events selected by the callback. The problem is that Android's RadioButtons are generally managed in the radiogroup, and the radiogroup is a linear layout, that is, one line is valid and multiple lines are invalid. As shown in the figure, two radiogroups are needed to cooperate. So what if we cooperate?

Analysis: the multi line display of radiogroup is actually the switching between two radiogroups, which is operated by using the clearcheck() method of radiogroup.

However, before using clearcheck (), if the radiogroup has set listening, the execution will report a stackoveflowerror error error.

The correct method is as follows:

Call first

setOnCheckedchangelistener(null)

Re call

Clearcheck () and then re set onchecked changelister (checkedlistener).

Case code

1. The layout is as follows:

<RadioGroup
      android:id="@+id/rg_manhole_state_one"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:layout_toRightOf="@id/tv_manhole_state"
      android:orientation="horizontal"
      android:paddingTop="@dimen/padding_5">

      <RadioButton
       android:id="@+id/rb_intact"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:button="@null"
       android:drawableLeft="@drawable/bg_radiobutten"
       android:drawablePadding="@dimen/padding_10"
       android:text="@string/intact"
       android:textColor="@color/white"
       android:textSize="@dimen/small_size" />

      <RadioButton
       android:id="@+id/rb_lose"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="30dp"
       android:button="@null"
       android:drawableLeft="@drawable/bg_radiobutten"
       android:drawablePadding="@dimen/padding_10"
       android:text="@string/lose"
       android:textColor="@color/white"
       android:textSize="@dimen/small_size" />

      <RadioButton
       android:id="@+id/rb_sunken"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="30dp"
       android:button="@null"
       android:drawableLeft="@drawable/bg_radiobutten"
       android:drawablePadding="@dimen/padding_10"
       android:text="@string/sunken"
       android:textColor="@color/white"
       android:textSize="@dimen/small_size" />
     </RadioGroup>
     <RadioGroup
     android:id="@+id/rg_manhole_state_two"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="@dimen/padding_100"
     android:layout_marginTop="5dp"
     android:orientation="horizontal"
     android:paddingTop="@dimen/padding_5">

     <RadioButton
      android:id="@+id/rb_occupation"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:button="@null"
      android:drawableLeft="@drawable/bg_radiobutten"
      android:drawablePadding="@dimen/padding_10"
      android:text="@string/occupation"
      android:textColor="@color/white"
      android:textSize="@dimen/small_size" />

     <RadioButton
      android:id="@+id/rb_damage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="30dp"
      android:button="@null"
      android:drawableLeft="@drawable/bg_radiobutten"
      android:drawablePadding="@dimen/padding_10"
      android:text="@string/damage"
      android:textColor="@color/white"
      android:textSize="@dimen/small_size" />

     <RadioButton
      android:id="@+id/rb_heave"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="30dp"
      android:button="@null"
      android:drawableLeft="@drawable/bg_radiobutten"
      android:drawablePadding="@dimen/padding_10"
      android:text="@string/heave"
      android:textColor="@color/white"
      android:textSize="@dimen/small_size" />
</RadioGroup>

2. The specific operations are as follows:

Declare control:

@InjectView(R.id.rg_manhole_state_one)
RadioGroup rgManholeStateOne;
@InjectView(R.id.rg_manhole_state_two)
RadioGroup rgManholeStateTwo;

Set listening:

rgManholeStateOne.setOnCheckedchangelistener(new OnMyManholeStateOneCheckedchangelistener());
rgManholeStateTwo.setOnCheckedchangelistener(new OnMyManholeStateTwoCheckedchangelistener());

Implement single selection:

private class OnMyManholeStateOneCheckedchangelistener implements RadioGroup.OnCheckedchangelistener {

  @Override
  public void onCheckedChanged(RadioGroup radioGroup,int position) {
   switch (position) {
    case R.id.rb_intact:
     if (rbIntact.isChecked())
      rgManholeStateTwo.clearCheck();
     break;
    case R.id.rb_lose:
     if (rbLose.isChecked())
      rgManholeStateTwo.clearCheck();
     break;
    case R.id.rb_sunken:
     if (rbSunken.isChecked())
      rgManholeStateTwo.clearCheck();
     break;
   }
  }
 }

 private class OnMyManholeStateTwoCheckedchangelistener implements RadioGroup.OnCheckedchangelistener {

  @Override
  public void onCheckedChanged(RadioGroup radioGroup,int position) {

   switch (position) {
    case R.id.rb_occupation:
     if (rbOccupation.isChecked())
      rgManholeStateOne.clearCheck();
     break;
    case R.id.rb_damage:
     if (rbDamage.isChecked())
      rgManholeStateOne.clearCheck();
     break;
    case R.id.rb_heave:
     if (rbHeave.isChecked())
      rgManholeStateOne.clearCheck();
     break;
   }
  }
 }

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
分享
二维码
< <上一篇
下一篇>>