薄荷腿子

保持愚蠢,又不能知道自己有多蠢。——知行合一


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

自定义单项选择器和多项选择器

发表于 2016-09-06 |

单项选择器

单项选择器采用继承RadioGroup,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class DayChooseRadioGroup extends RadioGroup {
private Context mContext;
// holds the checked id; the selection is empty by default
private int mCheckedId = -1;
private int mRadioWith;
private int mRadioHeight;
// 存放当前的radioButton
private String mData[] = {"2", "3", "4", "5", "6", "7"};
public DayChooseRadioGroup(Context context) {
this(context, null);
}
public DayChooseRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
setOrientation(HORIZONTAL);
mContext = context;
mRadioWith = (DrawUtil.sWidthPixels - DrawUtil.dip2px(30) - 6 * DrawUtil.dip2px(13)) / 7;
mRadioHeight = mRadioWith;
int margin = (DrawUtil.sWidthPixels - mRadioWith * mData.length - DrawUtil.dip2px(30)) / (mData.length - 1);
LayoutParams params = new LayoutParams(mRadioWith, mRadioHeight);
params.rightMargin = margin;
params.gravity = Gravity.CENTER;
for (int i = 0; i < mData.length; i++) {
RadioButton radioButton = new RadioButton(mContext);
radioButton.setLayoutParams(params);
radioButton.setButtonDrawable(android.R.color.transparent);
radioButton.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
radioButton.setText(mData[i]);
radioButton.setGravity(Gravity.CENTER);
radioButton.setPadding(0, 0, 0, 0);
radioButton.setTextSize(21);
radioButton.setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector));
addView(radioButton);
}
}
/**
* 初始化设置默认选中
*/
public void setCheckView(int checkedId) {
for (int i = 0; i < getChildCount(); i++) {
if (i == (checkedId - 2)) {
this.check(getChildAt(i).getId());
}
}
}
/**
* 不可点击
*/
public void disableRadioGroup() {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).setEnabled(false);
if (getChildAt(i).getId() == getCheckedRadioButtonId()) {
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_background_selector));
} else {
((RadioButton) getChildAt(i)).setTextColor(0xffc4c6cb);
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_circle_selector));
}
}
}
/**
* 使能可点击
*/
public void enableRadioGroup() {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).setEnabled(true);
if (getChildAt(i).getId() == getCheckedRadioButtonId()) {
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
} else {
((RadioButton) getChildAt(i)).setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector));
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
}
}
}
}

多项选择器

多项选择器采用复选控件CheckBox, 继承LinearLayout,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public class DayChooseCheckBox extends LinearLayout {
private Context mContext;
private int mRadioWith;
private int mRadioHeight;
private String mData[] = {"一", "二", "三", "四", "五", "六", "日"};
List<Integer> mChooseDays = new ArrayList<>();
private CheckedStateTracker mChildOnCheckedChangeListener;
private OnCheckedChangeListener mOnCheckedChangeListener;
public DayChooseCheckBox(Context context) {
this(context, null);
}
public DayChooseCheckBox(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DayChooseCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setOrientation(HORIZONTAL);
mContext = context;
mChildOnCheckedChangeListener = new CheckedStateTracker();
mRadioWith = (DrawUtil.sWidthPixels - DrawUtil.dip2px(30) - (mData.length - 1) * DrawUtil.dip2px(13)) / mData.length;
mRadioHeight = mRadioWith;
LayoutParams params = new LayoutParams(mRadioWith, mRadioHeight);
params.rightMargin = DrawUtil.dip2px(13);
params.gravity = Gravity.CENTER;
for (int i = 0; i < mData.length; i++) {
CheckBox checkBox = new CheckBox(mContext);
checkBox.setLayoutParams(params);
checkBox.setButtonDrawable(android.R.color.transparent);
checkBox.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
checkBox.setText(mData[i]);
checkBox.setTag(i);
checkBox.setOnCheckedChangeListener(mChildOnCheckedChangeListener);
checkBox.setGravity(Gravity.CENTER);
checkBox.setPadding(0, 0, 0, 0);
checkBox.setTextSize(18);
checkBox.setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector));
addView(checkBox);
}
}
/**
* 初始化设置默认选中
*/
public void setCheckViews(List<Integer> checkedIds) {
for (int i = 0; i < getChildCount(); i++) {
for (int j = 0; j < checkedIds.size(); j++) {
if (i == (checkedIds.get(j) - 1)) {
((CheckBox) getChildAt(i)).setChecked(true);
}
}
}
}
/**
* 不可点击
*/
public void disableRadioGroup() {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).setEnabled(false);
if (((CheckBox) getChildAt(i)).isChecked()) {
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_background_selector));
} else {
((CheckBox) getChildAt(i)).setTextColor(0xffc4c6cb);
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_choose_disable_circle_selector));
}
}
}
/**
* 使能可点击
*/
public void enableRadioGroup() {
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).setEnabled(true);
if (((CheckBox) getChildAt(i)).isChecked()) {
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
} else {
((CheckBox) getChildAt(i)).setTextColor(getResources().getColorStateList(R.color.goal_day_choose_text_selector));
getChildAt(i).setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.goal_day_choose_background_selector));
}
}
}
/**
* 将结果回调到外部使用
*/
public interface OnCheckedChangeListener {
void onCheckedChanged(DayChooseCheckBox group, List<Integer> chooseDays);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int day = (int) buttonView.getTag() + 1;
if (isChecked) {
mChooseDays.add(day);
} else {
Iterator<Integer> iterator = mChooseDays.iterator();
while (iterator.hasNext()) {
int b = iterator.next();
if (b == day) {
iterator.remove();
}
}
}
Log.i("zou", "<DayChooseCheckBox> mOnCheckedChangeListener day= " + day + " mChooseDays.size() = " + mChooseDays.size());
setCheckedId(mChooseDays);
}
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
private void setCheckedId(List<Integer> chooseDays) {
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, chooseDays);
}
}
}

Introduce

发表于 2016-09-05 |

This is my Blog. And this is my first time to create blog by myself. All I want to do is record my life and improve myself.

First Record

This is just a test to record first hexo.

1.Run server

1
$ hexo server

2.Generate static files

1
$ hexo generate

3.Deploy to remote sites

1
$ hexo deploy

一些常用命令

1
2
3
4
5
6
7
$ hexo new "postName" #新建文章
$ hexo new page "pageName" #新建页面
$ hexo generate #生成静态页面至public目录
$ hexo server #开启预览访问端口(默认端口4000,'ctrl + c'关闭server)
$ hexo deploy #将.deploy目录部署到GitHub
$ hexo help #查看帮助
$ hexo version #查看Hexo的版本
薄荷腿子的Alsot

薄荷腿子的Alsot

向着人工智能AI、机器学习ML迈进

2 日志
© 2017 薄荷腿子的Alsot
由 Hexo 强力驱动
主题 - NexT.Pisces