Android小白_努力中自定义views Android知识

Android DatePicker,NumPicker解析(分

2017-07-21  本文已影响1572人  秦子帅

推荐文章:
Android实现购物车页面及购物车效果(点击动画)

Android自定义收银键盘(原创)

源码下载地址

一. Android提供了一个选择日期的控件——DatePicker,基本的用法就不说了,在用的过程中会出现几个问题,分割线的颜色无法改变,字体大小,以及DatePicker的整体大小调整,下面就来解决这些问题.。

1.更改DatePicker分割线的颜色

效果图如下:


date_1.jpg

关键代码如下:

   /**
     * 设置时间选择器的分割线颜色
     *
     * @param datePicker
     */
    private void setDatePickerDividerColor(DatePicker datePicker) {
        // Divider changing:

        // 获取 mSpinners
        LinearLayout llFirst = (LinearLayout) datePicker.getChildAt(0);

        // 获取 NumberPicker
        LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0);
        for (int i = 0; i < mSpinners.getChildCount(); i++) {
            NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i);

            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, new ColorDrawable(Color.parseColor("#bd1b21")));//设置分割线颜色
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (Resources.NotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

2.更改字体大小: ---直接在style中写textsize属性

  <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:textSize">10sp</item>
    </style>

3.DatePicker的整体大小调整:-- resizePikcer(datePicker);

效果图如下:

date_2.jpg
   /**
     * 调整FrameLayout大小
     *
     * @param tp
     */
    private void resizePikcer(FrameLayout tp) {
        List<NumberPicker> npList = findNumberPicker(tp);
        for (NumberPicker np : npList) {
            resizeNumberPicker(np);
        }
    }
  /**
     * 得到viewGroup里面的numberpicker组件
     *
     * @param viewGroup
     * @return
     */
    private List<NumberPicker> findNumberPicker(ViewGroup viewGroup) {
        List<NumberPicker> npList = new ArrayList<NumberPicker>();
        View child = null;
        if (null != viewGroup) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                child = viewGroup.getChildAt(i);
                if (child instanceof NumberPicker) {
                    npList.add((NumberPicker) child);
                } else if (child instanceof LinearLayout) {
                    List<NumberPicker> result = findNumberPicker((ViewGroup) child);
                    if (result.size() > 0) {
                        return result;
                    }
                }
            }
        }
        return npList;
    }
  /*
         * 调整大小
         */
    private void resizeNumberPicker(NumberPicker np) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(280, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 0, 10, 0);
        np.setLayoutParams(params);
    }

二. NumPicker是一个数字选择器,下面通过两个NumPicker来实现时间选择器,并且修改分割线的颜色(主要实现了NumberPicker.Formatter和OnValueChangeListener接口):

效果图:

num_1.jpg
/*
更改分割线颜色
 */
    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值 透明
                    pf.set(picker, new ColorDrawable(this.getResources().getColor(android.R.color.holo_red_dark)));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    @Override
    public String format(int value) {
        String tmpStr = String.valueOf(value);
        if (value < 10) {
            tmpStr = "0" + tmpStr;
        }
        return tmpStr;
    }

  private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            Log.e("时:", newVal + "");
        }
    };
    private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            Log.e("分:", newVal + "");
        }
    };
   /*
        时间
         */
        hourpicker.setFormatter(this);
        hourpicker.setOnValueChangedListener(mOnHourChangedListener);
        hourpicker.setMaxValue(24);
        hourpicker.setMinValue(0);
        hourpicker.setValue(0);

        minuteicker.setOnValueChangedListener(mOnMinuteChangedListener);
        minuteicker.setFormatter(this);
        minuteicker.setMaxValue(60);
        minuteicker.setMinValue(0);
        minuteicker.setValue(0);
上一篇下一篇

猜你喜欢

热点阅读