我是靠谱客的博主 忧郁裙子,最近开发中收集的这篇文章主要介绍android timepicker 秒,在Android中以分钟间隔显示TimePicker,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用以下自定义类CustomTimePickerDialog,我认为解决你的问题。

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.List;

import android.app.TimePickerDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.widget.NumberPicker;

import android.widget.TimePicker;

public class CustomTimePickerDialog extends TimePickerDialog {

private final static int TIME_PICKER_INTERVAL = 5;

private TimePicker mTimePicker;

private final OnTimeSetListener mTimeSetListener;

public CustomTimePickerDialog(Context context, OnTimeSetListener listener,

int hourOfDay, int minute, boolean is24HourView) {

super(context, TimePickerDialog.THEME_HOLO_LIGHT, null, hourOfDay,

minute / TIME_PICKER_INTERVAL, is24HourView);

mTimeSetListener = listener;

}

@Override

public void updateTime(int hourOfDay, int minuteOfHour) {

mTimePicker.setCurrentHour(hourOfDay);

mTimePicker.setCurrentMinute(minuteOfHour / TIME_PICKER_INTERVAL);

}

@Override

public void onClick(DialogInterface dialog, int which) {

switch (which) {

case BUTTON_POSITIVE:

if (mTimeSetListener != null) {

mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),

mTimePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);

}

break;

case BUTTON_NEGATIVE:

cancel();

break;

}

}

@Override

public void onAttachedToWindow() {

super.onAttachedToWindow();

try {

Class> classForid = Class.forName("com.android.internal.R$id");

Field timePickerField = classForid.getField("timePicker");

mTimePicker = (TimePicker) findViewById(timePickerField.getInt(null));

Field field = classForid.getField("minute");

NumberPicker minuteSpinner = (NumberPicker) mTimePicker

.findViewById(field.getInt(null));

minuteSpinner.setMinValue(0);

minuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);

List displayedValues = new ArrayList<>();

for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {

displayedValues.add(String.format("%02d", i));

}

minuteSpinner.setDisplayedValues(displayedValues

.toArray(new String[displayedValues.size()]));

} catch (Exception e) {

e.printStackTrace();

}

}

}

这里是演示截图。

最后

以上就是忧郁裙子为你收集整理的android timepicker 秒,在Android中以分钟间隔显示TimePicker的全部内容,希望文章能够帮你解决android timepicker 秒,在Android中以分钟间隔显示TimePicker所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部