//有一个bug待解决,点击选中或者点击view有时候会把修改恢复之前的样式,有解决方案的还请大家给出参考,目前这样也算好很多了 public class ChargeNumberPicker extends NumberPicker { public ChargeNumberPicker(Context context) { super(context); } public ChargeNumberPicker(Context context, AttributeSet attrs) { super(context, attrs); } public ChargeNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void addView(View child) { super.addView(child); updateView(child); } @Override public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) { super.addView(child, index, params); updateView(child); } @Override public void addView(View child, android.view.ViewGroup.LayoutParams params) { super.addView(child, params); updateView(child); } public void updateView(View view) { if (view instanceof EditText) { //这里修改字体的属性 ((EditText) view).setTextSize(42); ((EditText) view).setTextColor(ContextCompat.getColor(view.getContext(), R.color.white)); view.setEnabled(false); view.setClickable(false); view.setFocusable(false); } } //可以修改默认选中样式 @Override public void onDrawForeground(Canvas canvas) { super.onDrawForeground(canvas); try { Field field = Objects.requireNonNull(getClass().getSuperclass()) .getDeclaredField("mInputText"); field.setAccessible(true); ((EditText) field.get(this)).setTextSize(48); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } //修改中间选中的样式 public void modifySelectText() { for (int i = 0; i < this.getChildCount(); i++) { View child = this.getChildAt(i); if (child instanceof EditText) { ((EditText) child).setTextSize(48); ((EditText) child).setTextColor(Color.RED); child.setVisibility(View.VISIBLE); } } // try { // Field field = Objects.requireNonNull(getClass().getSuperclass()) // .getDeclaredField("mInputText"); // field.setAccessible(true); // ((EditText) field.get(this)).setTextSize(48); // ((EditText) field.get(this)).setTextColor(Color.RED); // ((EditText) field.get(this)).setVisibility(View.VISIBLE); // } catch (NoSuchFieldException | IllegalAccessException e) { // e.printStackTrace(); // } } }
//setOnValueChangedListener的调用 @NonNull private NumberPicker.OnValueChangeListener getOnValueChangedListener() { return new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { ((ChargeNumberPicker)picker).modifySelectText(); } }; }