由于最近的項(xiàng)目中多處需要用到多行多列的RadioButton船逮,而google原生的RadioGroup又不能實(shí)現(xiàn)畴嘶!因此就要自己動(dòng)手實(shí)現(xiàn)了~
本文首發(fā): MyBlog
注意:
這里所說(shuō)的RadioButton都是在代碼中動(dòng)態(tài)添加的!
看下效果圖:
1、開(kāi)始造輪子: 分為四步
重寫RadioGroup-->目的是使 RadioButton可以自動(dòng)換行
布局中引用MyRadioGroupAuto
代碼中根據(jù)數(shù)據(jù)動(dòng)態(tài)添加RadioButton,然后MyRadioGroup通過(guò)
addView
把RadioButton加進(jìn)去這里需要講下RadioButton的
幾個(gè)屬性
radioButton.setPadding(20, 0, screenWidth / 6, 0);// 設(shè)置文字距離按鈕圖片四周的距離
radioButton.setButtonDrawable(R.drawable.transfer_radiobutton_drawable); //點(diǎn)擊效果
radioButton.setTag(loanAndFeeList.get(i)); // 設(shè)置tag,可以存一些數(shù)據(jù)
radioButton.setTextSize(13); //默認(rèn)單位是 sp
radioButton.setHeight(50); //默認(rèn)單位是px
RadioButton clickRadioButton = (RadioButton) radioGroup.findViewById(checkedId); //通過(guò)RadioGroup對(duì)象獲取點(diǎn)擊的RadioButton組件
設(shè)置RadioGroup點(diǎn)擊事件
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數(shù)量
int childCount = getChildCount();
//設(shè)置Group的左邊距耗式,下面也會(huì)使用x計(jì)算每行所占的寬度
int x = 0;
//設(shè)置Group的上邊距,下面也會(huì)使用y計(jì)算Group所占的高度
int y = 30;
//設(shè)置Group的行數(shù)
int row = 0;
for (int index = 0; index < childCount; index++) {
final View child = getChildAt(index);
if (child.getVisibility() != View.GONE) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//重新計(jì)算child的寬高
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
//添加到X中筹吐,(width+10) 設(shè)置child左邊距
x += (width + 10);
//行數(shù)*child高度+這次child高度=現(xiàn)在Group的高度,(height + 10)設(shè)置child上邊距
y = row * (height + 10) + (height + 10);
//當(dāng)前行寬X大于Group的最大寬度時(shí),進(jìn)行換行
if (x > maxWidth) {
//當(dāng)index不為0時(shí)秘遏,進(jìn)行row++丘薛,防止FirstChild出現(xiàn)大于maxWidth時(shí),提前進(jìn)行row++
if (index != 0)
row++;
//child的width大于maxWidth時(shí),重新設(shè)置child的width為最大寬度
if (width >= maxWidth) {
width = maxWidth - 30;
}
//重新設(shè)置當(dāng)前X
x = (width + 20);
//重新設(shè)置現(xiàn)在Group的高度
y = row * (height + 10) + (height + 10);
}
}
}
// 設(shè)置容器所需的寬度和高度
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 < childCount; 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
<com.ztesoft.zsmart.datamall.app.widget.MyRadioGroupAuto
android:id="@+id/my_radio_group_auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="33dp"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:layout_marginTop="16dp">
</com.ztesoft.zsmart.datamall.app.widget.MyRadioGroupAuto>
- 代碼中動(dòng)態(tài)添加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<String> 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文字長(zhǎng)度一致,跟最長(zhǎng)的保持一致!)
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 < j ? min : j;
}
String maxS = String.valueOf(max);
int maxLen = maxS.length();
for (int i = 0; i < loanList.length; i++) {
loanAndFeeList.add( loanList[i] + "," + loanFeeList[i]);
}
rgp = (RadioGroupAuto) findViewById(R.id.RadioGroup01);
int len = loanAndFeeList.size();
for (int j = 0; j < len; j++) {
RadioButton radioButton = new RadioButton(this);
radioButton.setPadding(20, 0, screenWidth / 6, 0); // 設(shè)置文字距離按鈕四周的距離
radioButton.setButtonDrawable(R.drawable.transfer_radiobutton_drawable);
String newLoanList = loanList[j];
if (loanList[j].length() < maxLen) {
newLoanList = newLoanList + appendLength(maxLen - loanList[j].length());
// 實(shí)現(xiàn) TextView同時(shí)顯示兩種風(fēng)格文字 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); //設(shè)置RadioButton的id
radioButton.setTag(loanAndFeeList.get(j)); //tag用于存儲(chǔ)RadioButton的一些數(shù)據(jù)
radioButton.setTextSize(13); //默認(rèn)單位是 sp
radioButton.setHeight(50); //默認(rèn)單位是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();
}
});
//根據(jù)這個(gè)來(lái)設(shè)置默認(rèn)選中的項(xiàng), 注意,這個(gè)要設(shè)置在監(jiān)聽(tīng)之后!,否則默認(rèn)點(diǎn)擊監(jiān)聽(tīng)不到!雖然有選中效果
//參考 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);
}
/**
* 補(bǔ)全長(zhǎng)度,保持最長(zhǎng)的長(zhǎng)度
*
* @param count 字符串長(zhǎng)度
* @return 補(bǔ)全后的長(zhǎng)度
* 這里長(zhǎng)度不夠的就用 "s" 占位, 賦值的時(shí)候?qū)⒆煮w設(shè)置白色!
*/
public String appendLength(int count) {
String st = "";
if (count < 0) {
count = 0;
}
for (int i = 0; i < count; i++) {
st = st + "s";
}
return st;
}
}