TimeNumberPicker.java
5 KB
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
143
144
package com.sw.laryngoscope.widget;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;
import com.sw.laryngoscope.R;
import java.lang.reflect.Field;
public class TimeNumberPicker extends android.widget.NumberPicker {
public TimeNumberPicker(Context context) {
super(context);
setPickerDividerColor(this);
}
public TimeNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
setPickerDividerColor(this);
}
public TimeNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setPickerDividerColor(this);
}
@Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
@Override
public void addView(View child, int index) {
super.addView(child, index);
updateView(child);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
public void updateView(View view) {
if (view instanceof EditText) {
// 修改字体的属性
//((EditText)view).setTextColor(Color.parseColor("#333333"));
((EditText)view).setTextColor(Color.parseColor("#008AAC"));
((EditText)view).setTextSize(16);
}
//setMInputStyle(16.0f);
//setNumberPickerTextColor(this, R.color.user_item_unsel_color);
}
// 通过反射拿到 mSelectionDivider 属性,然后设置上颜色值。
private void setPickerDividerColor(android.widget.NumberPicker mNumberPicker) {
Field[] pickerFields = android.widget.NumberPicker.class.getDeclaredFields();
for (Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try{
pf.set(mNumberPicker,new ColorDrawable(this.getResources().getColor(R.color.color_black)));
}catch (IllegalAccessException | Resources.NotFoundException | IllegalArgumentException e) {
e.printStackTrace();
}
}
if (pf.getName().equals("mSelectionDividerHeight")){
pf.setAccessible(true);
try {
int result = 0;
// pf.set(mNumberPicker.getHeight(),40);
pf.set(mNumberPicker, result);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
public void setNumberPickerTextColor(NumberPicker numberPicker) {
int color=Color.parseColor("#008AAC");
final int count = numberPicker.getChildCount();
for(int i = 0; i < count; i++){
View child = numberPicker.getChildAt(i);
if(child instanceof EditText){
try{
Field selectorWheelPaintField = numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
((EditText)child).setTextColor(color);
numberPicker.invalidate();
}
catch(NoSuchFieldException e){
}
catch(IllegalAccessException e){
}
catch(IllegalArgumentException e){
}
}
}
}
//反射获取控件 mSelectionDivider mInputText当前选择的view
public Field getFile(String fieldName) {
try {
//设置分割线的颜色值
Field pickerFields = NumberPicker.class.getDeclaredField(fieldName);
pickerFields.setAccessible(true);
return pickerFields;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//设置选中控件的style 注意一点要在setOnValueChangedListener或 setOnScrollChangeListener,setOnScrollListener中调
// 用picker.performClick();两种情况动画效果不同
//同时注意他的代用位置 要在当前控件初始化之后 否则获得不了
public void setMInputStyle(Float size) {
Field mInputText = this.getFile("mInputText");
try {
((EditText) mInputText.get(this)).setTextColor(getResources().getColor(R.color.color_black));
((EditText) mInputText.get(this)).setTextSize(size);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}