热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实现多行多列的RadioButton

由于最近的项目中多处需要用到多行多列的RadioButton,而google原生的RadioGroup又不能实现!因此就要自己动手实现了~本文首发:MyBlog注意:这里所说的Ra

由于最近的项目中多处需要用到多行多列的RadioButton,而google原生的RadioGroup又不能实现!因此就要自己动手实现了~

本文首发: MyBlog

注意:这里所说的RadioButton都是在代码中动态添加的!

看下效果图:

《实现多行多列的RadioButton》 实现多行多列的RadioButton

《实现多行多列的RadioButton》 行列对齐RadioButton

1、开始造轮子: 分为四步
  • 重写RadioGroup–>目的是使 RadioButton可以自动换行

  • 布局中引用MyRadioGroupAuto

  • 代码中根据数据动态添加RadioButton,然后MyRadioGroup通过addView把RadioButton加进去

  • 这里需要讲下RadioButton的几个属性

  • radioButton.setPadding(20, 0, screenWidth / 6, 0);// 设置文字距离按钮图片四周的距离

  • radioButton.setButtonDrawable(R.drawable.transfer_radiobutton_drawable); //点击效果

  • radioButton.setTag(loanAndFeeList.get(i)); // 设置tag,可以存一些数据

  • radioButton.setTextSize(13); //默认单位是 sp

  • radioButton.setHeight(50); //默认单位是px

  • RadioButton clickRadioButton = (RadioButton) radioGroup.findViewById(checkedId); //通过RadioGroup对象获取点击的RadioButton组件

  • 设置RadioGroup点击事件

2、下面直接上代码: 三步
  • 自定义MyRadioGroupAuto

/**
* Author: 0027008122 [yang.jianan@zte.com.cn]
* Time: 2016-08-02 11:33
* Version: 1.0
* TaskId:
* Description:
*/
public class MyRadioGroupAuto extends RadioGroup {
public MyRadioGroupAuto(Context context) {
super(context);
}
public MyRadioGroupAuto(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取最大宽度
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
//获取Group中的Child数量
int childCount = getChildCount();
//设置Group的左边距,下面也会使用x计算每行所占的宽度
int x = 0;
//设置Group的上边距,下面也会使用y计算Group所占的高度
int y = 30;
//设置Group的行数
int row = 0;
for (int index = 0; index final View child = getChildAt(index);
if (child.getVisibility() != View.GONE) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//重新计算child的宽高
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
//添加到X中,(width+10) 设置child左边距
x += (width + 10);
//行数*child高度+这次child高度=现在Group的高度,(height + 10)设置child上边距
y = row * (height + 10) + (height + 10);
//当前行宽X大于Group的最大宽度时,进行换行
if (x > maxWidth) {
//当index不为0时,进行row++,防止FirstChild出现大于maxWidth时,提前进行row++
if (index != 0)
row++;
//child的width大于maxWidth时,重新设置child的width为最大宽度
if (width >= maxWidth) {
width = maxWidth - 30;
}
//重新设置当前X
x = (width + 20);
//重新设置现在Group的高度
y = row * (height + 10) + (height + 10);
}
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(maxWidth, y);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
int maxWidth = r - l;
int x = 10;
int y = 0;
int row = 0;
for (int i = 0; i final View child = this.getChildAt(i);
if (child.getVisibility() != View.GONE) {
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += (width + 10);
y = row * (height + 10) + (height + 10);
if (x > maxWidth) {
if (i != 0)
row++;
if (width >= maxWidth) {
width = maxWidth - 30;
}
x = (width + 20);
y = row * (height + 10) + (height + 10);
}
child.layout(x - width, y - height, x, y);
}
}
}
}

  • 布局中引用MyRadioGroupAuto

android:id="@+id/my_radio_group_auto"
android:layout_
android:layout_
android:layout_marginBottom="33dp"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:layout_marginTop="16dp">

  • 代码中动态添加RadioButton

public class CheckboxRadiobuttonDemo extends Activity {
/**
* Called when the activity is first created.
*/
private RadioGroupAuto rgp;
private RadioGroup yuansheng;
private String[] loanList;
private String[] loanFeeList;
private List loanAndFeeList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取屏幕信息
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
loanAndFeeList = new ArrayList<>();
loanList = "800,1000,1600,200,300,500,700".split(",");
loanFeeList = "50,80,100,20,30,50,70".split(",");

//求最大最小值 (为了保持RadioButton文字长度一致,跟最长的保持一致!)
int max = Integer.parseInt(loanList[0]);
int min = Integer.parseInt(loanList[0]);
for (String i : loanList) {
int j = Integer.parseInt(i);
max = max > j ? max : j;
min = min }

String maxS = String.valueOf(max);
int maxLen = maxS.length();
for (int i = 0; i loanAndFeeList.add( loanList[i] + "," + loanFeeList[i]);
}
rgp = (RadioGroupAuto) findViewById(R.id.RadioGroup01);
int len = loanAndFeeList.size();
for (int j = 0; j RadioButton radioButton = new RadioButton(this);
radioButton.setPadding(20, 0, screenWidth / 6, 0); // 设置文字距离按钮四周的距离
radioButton.setButtonDrawable(R.drawable.transfer_radiobutton_drawable);
String newLoanList = loanList[j];
if (loanList[j].length() newLoanList = newLoanList + appendLength(maxLen - loanList[j].length());
// 实现 TextView同时显示两种风格文字 http://txlong-onz.iteye.com/blog/1142781
SpannableStringBuilder sb = new SpannableStringBuilder(newLoanList);
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.WHITE);
sb.setSpan(fcs, loanList[j].length(), maxLen, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
radioButton.setText(sb);
} else {
newLoanList = loanList[j];
radioButton.setText(newLoanList);
}

radioButton.setId(j); //设置RadioButton的id
radioButton.setTag(loanAndFeeList.get(j)); //tag用于存储RadioButton的一些数据
radioButton.setTextSize(13); //默认单位是 sp
radioButton.setHeight(50); //默认单位是px
rgp.addView(radioButton); //添加到RadioGroup中
}
rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton clickRadioButton = (RadioButton) group.findViewById(checkedId);
String tipInfo = "id: " + clickRadioButton.getId() + " text: " + clickRadioButton.getText() +
/*"hint: " + clickRadioButton.getHint() +*/ " tag:" + clickRadioButton.getTag();
System.out.println(tipInfo);
Toast.makeText(CheckboxRadiobuttonDemo.this, tipInfo,
Toast.LENGTH_SHORT).show();
}
});
//根据这个来设置默认选中的项, 注意,这个要设置在监听之后!,否则默认点击监听不到!虽然有选中效果
//参考 http://blog.csdn.net/lzqjfly/article/details/16963645
//以及http://stackoverflow.com/questions/9175635/how-to-set-radio-button-checked-as-default-in-radiogroup-with-android
rgp.check(0);
}
/**
* 补全长度,保持最长的长度
*
* @param count 字符串长度
* @return 补全后的长度
* 这里长度不够的就用 "s" 占位, 赋值的时候将字体设置白色!
*/
public String appendLength(int count) {
String st = "";
if (count <0) {
count = 0;
}
for (int i = 0; i st = st + "s";
}
return st;
}
}

推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • qt学习(六)数据库注册用户的实现方法
    本文介绍了在qt学习中实现数据库注册用户的方法,包括登录按钮按下后出现注册页面、账号可用性判断、密码格式判断、邮箱格式判断等步骤。具体实现过程包括UI设计、数据库的创建和各个模块调用数据内容。 ... [详细]
  • 解决Cydia数据库错误:could not open file /var/lib/dpkg/status 的方法
    本文介绍了解决iOS系统中Cydia数据库错误的方法。通过使用苹果电脑上的Impactor工具和NewTerm软件,以及ifunbox工具和终端命令,可以解决该问题。具体步骤包括下载所需工具、连接手机到电脑、安装NewTerm、下载ifunbox并注册Dropbox账号、下载并解压lib.zip文件、将lib文件夹拖入Books文件夹中,并将lib文件夹拷贝到/var/目录下。以上方法适用于已经越狱且出现Cydia数据库错误的iPhone手机。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
author-avatar
此恨缠绵_793
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有