RadioGroup实现单选框的多行排列

 更新时间:2019年11月16日 13:02:37   作者:PK_night  
这篇文章主要为大家详细介绍了RadioGroup实现单选框的多行排列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

RadioGroup的使用非常简单,只是一般情况下,只能是横向排列或竖向排列.如果让多横排列的的就不是那么简单的了。

也许有童鞋该说了,将RadioButton写到LineLayout中不久行了吗?经过检验确实可以那样做,刚开始我也是这样做到.不过运行起来发现了了一个bug---单选按钮不在是单选了.而且选择事件不会被监听到.这就要求我们去想办法了.其实实现起来也不难.只要多用几个RadioGroup就可以了(要在代码中处理一些事件)。

上代码:

1.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>

这样就实现了多行布局,这只是我布局中的一部分,其中 android:textSize=”@dimen/RB_text_size” 为自己定义的字体大小.

2.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();
   }
  }
 }
}

已经搞定.还有一种方法就是自定义RadioGroup实现,不过这种有点复杂.我还是下班回家了.

补充:

使用RadioGroup.setcheck(RadioButton的id)初始化默认选中A按钮,但是监听不会执行的问题

解决:因为已经给A按钮在布局中设置了check=”true”; 将这个属性去掉就会执行监听了.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Android数据存储—使用SQLite数据库

    详解Android数据存储—使用SQLite数据库

    本篇文章主要介绍了详解Android数据存储—使用SQLite数据库,具有一定的参考价值,有兴趣的可以了解一下。
    2017-03-03
  • Android开发Dart Constructors构造函数使用技巧整理

    Android开发Dart Constructors构造函数使用技巧整理

    这篇文章主要为大家介绍了Android开发Dart Constructors构造函数使用技巧整理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Android Mainline基础简介

    Android Mainline基础简介

    Android Mainline 是通过模块化更新 Android 核心组件的框架,可能提高安全性,本文给大家介绍Android Mainline基础简介,感兴趣的朋友一起看看吧
    2025-04-04
  • AndroidStudio中重载方法@Override的使用详解

    AndroidStudio中重载方法@Override的使用详解

    这篇文章主要介绍了AndroidStudio中重载方法@Override的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • android实现可拖动的浮动view

    android实现可拖动的浮动view

    这篇文章主要为大家详细介绍了android实现可拖动的浮动view,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 详解Android如何自定义view实现圆形进度条

    详解Android如何自定义view实现圆形进度条

    Android中实现进度条有很多种方式,自定义进度条一般是继承progressBar或继承view来实现。本文将介绍通过自定义View实现的圆形进度条,感兴趣的可以动手试一试
    2022-01-01
  • Android实现搜索功能并本地保存搜索历史记录

    Android实现搜索功能并本地保存搜索历史记录

    这篇文章主要为大家详细介绍了Android实现搜索功能,并实现本地保存搜索历史记录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 小心!Listview结合EditText使用实例中遇到的那些坑

    小心!Listview结合EditText使用实例中遇到的那些坑

    小心!Listview结合EditText使用实例中遇到的那些坑,解决EditText焦点丢失、保存数据以及滚动冲突的问题,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android开发中Google为什么不让用Handler的runWithScissors()

    Android开发中Google为什么不让用Handler的runWithScissors()

    这篇文章主要介绍了Android开发中Google为什么不让用Handler的runWithScissors(),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Android中Canvas的常用方法总结

    Android中Canvas的常用方法总结

    在Android自定义View的时候,我们经常需要绘制一些自己想要的效果。这里就需要使用Canvas对象。下面这篇文章将Canvas对象常用方法做个笔记,方便自己和大家以后使用的时候查阅,下面来一起看看吧。
    2016-09-09

最新评论