TimePicker使用全解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wyg1230/article/details/81711520

目录

1.简单使用
2.指定字体颜色及大小
3.设置24小时显示格式,当前显示时间,禁止调起键盘,监听时间改变
4.设置分割线颜色,设置NumberPicker大小

简单显示

    <TimePicker
        android:id="@+id/time_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner"
        android:theme="@style/Theme.TimePicker">
    </TimePicker>

android:theme为指定字体颜色及大小用到的。

指定字体颜色及大小

在style文件中定义Style

    <style name="Theme.TimePicker" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:editTextStyle">@style/Widget.AppCompat.EditText.Gray</item>
    </style>

    <style name="Widget.AppCompat.EditText.Gray">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#b3b3b3</item>
    </style>

指定android:theme为定义的Style

一些设置

		mTimePicker.setIs24HourView(true);//设置为24小时显示格式
        mTimePicker.setCurrentHour(0); //当前小时
        mTimePicker.setCurrentMinute(30); //当前分钟
        mTimePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);//设置父布局focus,子控件不会focus,以此禁止调起键盘
        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                long delayMills = (hourOfDay * 60 + minute) * 60 * 1000;
            }
        });

指定分割线divider颜色,设定Picker大小
这里采用反射取得对应属性进行更改

	public void changeTimePickerColor() {
        Resources systemResources = Resources.getSystem();
        int hourNumberPickerId = systemResources.getIdentifier("hour", "id", "android");
        int minuteNumberPickerId = systemResources.getIdentifier("minute", "id", "android");

        NumberPicker hourNumberPicker = findViewById(hourNumberPickerId);
        NumberPicker minuteNumberPicker = findViewById(minuteNumberPickerId);

        setNumberPickerDivider(hourNumberPicker, Color.YELLOW);
        setNumberPickerDivider(minuteNumberPicker, Color.GREEN);
        setNumberpickerTextColour(hourNumberPicker, Color.RED);
        setNumberpickerTextColour(minuteNumberPicker, Color.BLUE);

//        setPickerSize(hourNumberPicker, 30, this);
    }

    //指定分割线颜色
    private void setNumberPickerDivider(NumberPicker numberPicker, int color) {

        try {
            Field dividerFields = NumberPicker.class.getDeclaredField("mSelectionDivider");

            dividerFields.setAccessible(true);

            dividerFields.set(numberPicker, new ColorDrawable(color));

        } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
            Log.w("setNumberPickerTxtClr", e);
        }
    }



    //另一种指定文字颜色的方法
    private void setNumberpickerTextColour(NumberPicker number_picker, int color) {
        final int count = number_picker.getChildCount();

        for (int i = 0; i < count; i++) {
            View child = number_picker.getChildAt(i);

            try {
                Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
                wheelpaint_field.setAccessible(true);
                ((Paint) wheelpaint_field.get(number_picker)).setColor(color);
                ((EditText) child).setTextColor(color);
                number_picker.invalidate();
            } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
                Log.i("setNumberPickerTxtClr", "set_numberpicker_text_colour: "+e);
            }
        }
    }


    //指定NumberPicker大小                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                改的NumberPicker和NumberPicker的宽度值
    private static void setPickerSize(NumberPicker np, int widthDpValue,Context context) {
        int widthPxValue = ToolUtils.dp2px(context, widthDpValue);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthPxValue, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        np.setLayoutParams(params);
    }

显示效果

展开阅读全文

没有更多推荐了,返回首页