我可以控制Android TimePicker视图,只显示15分钟的分钟数吗?
如果它现在是12:28,请显示12:30并单击+和 - 按钮将递增并递减15?
谢谢,
T恤
答案 0 :(得分:19)
这是我的版本,您可以在其中设置间隔:
private static final int TIME_PICKER_INTERVAL=15;
private boolean mIgnoreEvent=false;
private TimePicker.OnTimeChangedListener mTimePickerListener=new TimePicker.OnTimeChangedListener(){
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute){
if (mIgnoreEvent)
return;
if (minute%TIME_PICKER_INTERVAL!=0){
int minuteFloor=minute-(minute%TIME_PICKER_INTERVAL);
minute=minuteFloor + (minute==minuteFloor+1 ? TIME_PICKER_INTERVAL : 0);
if (minute==60)
minute=0;
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;
}
}
};
答案 1 :(得分:19)
创建一个xml文件并将其命名为activity_time_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutHourMinute"
android:layout_centerInParent="true"
/>
</RelativeLayout>
现在创建像这样的活动类
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.NumberPicker;
import android.widget.TimePicker;
public class TimePickerActivity extends Activity {
TimePicker timePicker;
private int TIME_PICKER_INTERVAL = 15;
NumberPicker minutePicker;
List<String> displayedValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_time_picker);
super.onCreate(savedInstanceState);
timePicker = (TimePicker)findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(0);
timePicker.setCurrentMinute(0);
setTimePickerInterval(timePicker);
}
@SuppressLint("NewApi")
private void setTimePickerInterval(TimePicker timePicker) {
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
// Field timePickerField = classForid.getField("timePicker");
Field field = classForid.getField("minute");
minutePicker = (NumberPicker) timePicker
.findViewById(field.getInt(null));
minutePicker.setMinValue(0);
minutePicker.setMaxValue(3);
displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
// for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
// displayedValues.add(String.format("%02d", i));
// }
minutePicker.setDisplayedValues(displayedValues
.toArray(new String[0]));
minutePicker.setWrapSelectorWheel(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:8)
以下对我有用。
首先,在onCreate:
pickStartTime = (TimePicker)findViewById(R.id.StartTime);
pickStartTime.setOnTimeChangedListener(mStartTimeChangedListener);
设置OnTimeChangedListener:
private TimePicker.OnTimeChangedListener mStartTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(view, startDate, hourOfDay, minute);
}
};
Null OnTimeChangedListener(在下面的updateDisplay中的注释中解释):
private TimePicker.OnTimeChangedListener mNullTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
};
private void updateDisplay(TimePicker timePicker, Date date, int hourOfDay, int minute) {
// do calculation of next time
int nextMinute = 0;
if (minute >= 45 && minute <= 59)
nextMinute = 45;
else if(minute >= 30)
nextMinute = 30;
else if(minute >= 15)
nextMinute = 15;
else if(minute > 0)
nextMinute = 0;
else {
nextMinute = 45;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(mNullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(nextMinute);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(mStartTimeChangedListener);
// update the date variable for use elsewhere in code
date.setMinutes(nextMinute);
}
答案 3 :(得分:3)
您必须编写自己的TimePicker。不确定是否可以扩展现有的TimePicker并自行处理按钮事件。
答案 4 :(得分:3)
我想我明白了。可能是贫民区但是......
这就是我所做的。
start_time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(hourOfDay, minute);
}
});
private void updateDisplay(int hourOfDay, int minute) {
// do calculation of next time
// nextTime = calculation of next time;
// the processed boolean is to prevent infinite loop
if (!processed) {
start_time.setCurrentMinute(nextTime);
processed = true;
} else {
processed = false;
}
}
答案 5 :(得分:3)
private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) {
int nextMinute = 0;
timePicker.setOnTimeChangedListener(mNullTimeChangedListener);
if (minute >= 45 && minute <= 59)
nextMinute = 45;
else if (minute >= 30)
nextMinute = 30;
else if (minute >= 15)
nextMinute = 15;
else if (minute > 0)
nextMinute = 0;
else {
nextMinute = 45;
}
if (minute - nextMinute == 1) {
if (minute >= 45 && minute <= 59)
nextMinute = 00;
else if(minute >= 30)
nextMinute = 45;
else if(minute >= 15)
nextMinute = 30;
else if(minute > 0)
nextMinute = 15;
else {
nextMinute = 15;
}
}
timePicker.setCurrentMinute(nextMinute);
timePicker.setOnTimeChangedListener(timePickerChangedListener);
}
更新为+和 - 按钮修改的显示方法。其余的代码与Andrew Dyer的代码相同。
答案 6 :(得分:2)
我已根据此问题的一些先前答案以及我自己的一些补充提出了解决方案。此解决方案特别覆盖getCurrentMinute和setCurrentMinute,默认情况下会返回分钟索引,而不是分钟值(即,如果您的分钟间隔为15且选择器显示分钟为30,则在没有此代码的情况下将返回2)。 / p>
此解决方案还将翻转大于最大间隔值的分钟数(如果间隔为15,则任何分钟数> 45将分钟值设置为0并增加小时值。)
注意:这已经过测试,适用于Android 4.4和5.0。
public class CustomIntervalTimePicker extends TimePicker {
private static final int TIME_PICKER_MINUTE_INTERVAL = 15;
private OnTimeChangedListener timeChangedListener;
public CustomIntervalTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
try {
Class<?> classForId = Class.forName("com.android.internal.R$id");
Field field = classForId.getField("minute");
NumberPicker minuteSpinner = (NumberPicker) this.findViewById(field.getInt(null));
minuteSpinner.setMaxValue((60 / TIME_PICKER_MINUTE_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<>();
for (int i = 0; i < 60; i += TIME_PICKER_MINUTE_INTERVAL)
displayedValues.add(String.format("%02d", i));
minuteSpinner.setDisplayedValues(displayedValues.toArray(new String[displayedValues.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
private int maxMinuteIndex() {
return (60 / TIME_PICKER_MINUTE_INTERVAL) - 1;
}
@Override
public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener) {
super.setOnTimeChangedListener(internalTimeChangedListener);
this.timeChangedListener = onTimeChangedListener;
}
@Override
public Integer getCurrentMinute() {
return super.getCurrentMinute() * TIME_PICKER_MINUTE_INTERVAL;
}
@Override
public void setCurrentMinute(Integer currentMinute) {
int cleanMinute = currentMinute / TIME_PICKER_MINUTE_INTERVAL;
if(currentMinute % TIME_PICKER_MINUTE_INTERVAL > 0) {
if(cleanMinute == maxMinuteIndex()) {
cleanMinute = 0;
setCurrentHour(getCurrentHour()+1);
} else {
cleanMinute++;
}
}
super.setCurrentMinute(cleanMinute);
}
// We want to proxy all the calls to our member variable OnTimeChangedListener with our own
// internal listener in order to make sure our overridden getCurrentMinute is called. Without
// this some versions of android return the underlying minute index.
private OnTimeChangedListener internalTimeChangedListener = new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
timeChangedListener.onTimeChanged(view, getCurrentHour(), getCurrentMinute());
}
};
}
答案 7 :(得分:1)
首先,我要感谢Andrew克服stackoverflow错误(无限循环)。我将时间增加15分钟的逻辑就像这样.Declare int previousMinuteSet = 0作为给定类中的成员变量。< / p>
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(mNullTimeChangedListener);
if((minute - previousMinuteSet == 1) || (minute - previousMinuteSet == 15) || (minute - previousMinuteSet == -45)) {
// set hour
timePicker.setCurrentHour((hourOfDay + minute / 45) % 12);
// set minute
timePicker.setCurrentMinute((minute + 14) % 60);
} else {
// set hour
timePicker.setCurrentHour(hourOfDay);
// set minute
timePicker.setCurrentMinute(minute);
}
previousMinuteSet = minute;
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(timePickerChangedListener);
if((minute - previousMinuteSet == 1) || (minute - previousMinuteSet == 15) || (minute - previousMinuteSet == -45)) {
// set hour
timePicker.setCurrentHour((hourOfDay + minute / 45) % 12);
// set minute
timePicker.setCurrentMinute((minute + 14) % 60);
} else {
// set hour
timePicker.setCurrentHour(hourOfDay);
// set minute
timePicker.setCurrentMinute(minute);
}
previousMinuteSet = minute;
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(timePickerChangedListener);
希望这可能会对其他人有所帮助。我在安德鲁的updateDisplay方法中替换的代码,除了一切都相同。
答案 8 :(得分:0)
这是我写的版本。它是C#代码,因为我使用的是Xamarin,但我希望任何人都难以将它翻译成Java,而不是对我来说,当我需要复制时,执行反向翻译一段Java代码。
它与标准的TimePicker略有不同,因为它的顶部和底部边框不是连续的,但它是一个有些人会发现微不足道的缺陷。
我可以更好地设置自定义分钟间隔,而不是让你在实现中对间隔值进行硬编码,但我相信如果你想非常快速的话,你可以改进这个解决方案!
<强> HourPickerMapper.cs 强>
public class HourPickerMapper
{
public HourPickerMapper() { }
public int MapValue(int hour)
{
return hour;
}
public int MapNumber(int number)
{
return number;
}
public void InitNumberPicker(NumberPicker numberPicker)
{
int[] numbers = new int[24];
for (var i = 0; i < numbers.Length; i++)
{
numbers[i] = i;
}
var displayed = numbers.Select(_ => $"{_:00}").ToArray();
numberPicker.SetDisplayedValues(displayed);
numberPicker.MinValue = 0;
numberPicker.MaxValue = displayed.Length - 1;
}
}
<强> MinutePickerMapper.cs 强>
public class MinutePickerMapper
{
public MinutePickerMapper() { }
public int MapValue(int value)
{
return (int)Math.Floor(value / 10.0);
}
public int MapNumber(int number)
{
return number * 10;
}
public void InitNumberPicker(NumberPicker numberPicker)
{
int[] numbers = new int[6];
for (var i = 0; i < numbers.Length; i++)
{
numbers[i] = i * 10;
}
var displayed = numbers.Select(_ => _.ToString()).ToArray();
numberPicker.SetDisplayedValues(displayed);
numberPicker.MinValue = 0;
numberPicker.MaxValue = displayed.Length - 1;
}
}
以下是使用示例,DialogFragment
public class CustomDateTimePickerDialogFragment : Android.Support.V4.App.DialogFragment
{
private DateTime _dt;
private NumberPicker _datePicker, _hourPicker, _minutePicker;
private DatePickerMapper _datePickerMapper;
private HourPickerMapper _hourPickerMapper;
private MinutePickerMapper _minutePickerMapper;
public event EventHandler<DateTimeSetEventArgs> DateTimeSetEvent;
public CustomDateTimePickerDialogFragment()
{
_dt = DateTime.Now + new TimeSpan(24, 0, 0); // now + 1 day
}
public CustomDateTimePickerDialogFragment(DateTime dt)
{
_dt = dt;
if (dt == null)
{
_dt = DateTime.Now + new TimeSpan(24, 0, 0); // now + 1 day
}
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Context);
builder.SetNeutralButton(Resource.String.dialog_datetime_now, (sender, e) =>
{
var eventArgs = new DateTimeSetEventArgs();
DateTimeSetEvent.Invoke(this, eventArgs);
});
builder.SetPositiveButton(Resource.String.dialog_ok, (sender, e) =>
{
var hour = _hourPickerMapper.MapNumber(_hourPicker.Value);
var minute = _minutePickerMapper.MapNumber(_minutePicker.Value);
var date = _datePickerMapper.MapNumber(_datePicker.Value);
var eventArgs = new DateTimeSetEventArgs(new DateTime(date.Year, date.Month, date.Day, hour, minute, 0));
DateTimeSetEvent.Invoke(this, eventArgs);
Dismiss();
});
var dialog = builder.Create();
dialog.SetView(createDateTimeView(dialog.LayoutInflater));
_datePicker.Value = _datePickerMapper.MapValue(_dt);
_hourPicker.Value = _hourPickerMapper.MapValue(_dt.Hour);
_minutePicker.Value = _minutePickerMapper.MapValue(_dt.Minute);
return dialog;
}
private View createDateTimeView(LayoutInflater inflater)
{
View view = inflater.Inflate(Resource.Layout.dialog_custom_datetime_picker, null);
_datePicker = view.FindViewById<NumberPicker>(Resource.Id.datePicker);
_datePickerMapper = new DatePickerMapper(DateTime.Now, 10);
_datePickerMapper.InitNumberPicker(_datePicker);
var now = DateTime.Now;
_datePicker.Value = _datePickerMapper.MapValue(now);
_minutePicker = view.FindViewById<NumberPicker>(Resource.Id.minutePicker);
_minutePickerMapper = new MinutePickerMapper();
_minutePickerMapper.InitNumberPicker(_minutePicker);
_minutePicker.Value = _minutePickerMapper.MapValue(now.Minute);
_hourPicker = view.FindViewById<NumberPicker>(Resource.Id.hourPicker);
_hourPickerMapper = new HourPickerMapper();
_hourPickerMapper.InitNumberPicker(_hourPicker);
_hourPicker.Value = _hourPickerMapper.MapValue(now.Hour);
return view;
}
public class DateTimeSetEventArgs : EventArgs
{
public DateTimeSetEventArgs(DateTime dt)
{
IsNow = false;
DT = dt;
}
public DateTimeSetEventArgs()
{
IsNow = true;
}
public DateTime DT { get; }
public bool IsNow { get; }
}
}
<强> dialog_custom_datetime_picker.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/dialog_padding"
>
<!-- Layout for the DatePicker -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<NumberPicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
/>
<NumberPicker
android:id="@+id/hourPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
/>
<NumberPicker
android:id="@+id/minutePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 9 :(得分:0)
TimePicker
基本上由两个NumberPicker
组成,持续数小时和数分钟。使用Kotlin,您可以使用此功能使用任何分钟间隔:
https://stackoverflow.com/a/62172660/2016262
答案 10 :(得分:0)
这是我在用户选择小时后仅显示:00,:15,:30和:45分钟选项的方式。
TimePickerDialog tpd = TimePickerDialog.newInstance( 这个, DateTime.now()。get(Calendar.HOUR_OF_DAY), DateTime.now()。get(Calendar.MINUTE), 真正); tpd.setTimeInterval(1,15);
tpd.setOkText(getString(R.string.lbl_ok));
tpd.setCancelText(getString(R.string.lbl_cancel));
tpd.show(getSupportFragmentManager(),tagName);