我用微调器格式的日期选择器和时间选择器构建了一个视图。一切似乎都运行良好,只是我不知道如何将时间间隔设置为5分钟?
我已经看到了如何使用DialogFragment进行此操作的示例,并使用DialogFragments通过Date Picker或Time Picker启动了单独的活动,但是我试图同时创建一个活动。
我需要修改当前代码以更改分钟间隔,或者在我的代码中合并一个DialogFragment,该代码与嵌入到视图中的“时间选择器”一起使用。
我找不到任何一个如何做的例子。任何建议,将不胜感激。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker
//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker
simpleTimePicker.setIs24HourView(true);// set to 24 hour
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}
public void cancelBTN(View view) {
}
public void saveBTN(View view) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/cancelBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="@color/gray"
android:onClick="cancelBTN"
android:text="Cancel"
android:textColor="@color/white" />
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/gray"
android:gravity="center"
android:text="Select Date - Time"
android:textColor="@color/white"
android:textSize="20dp" />
<Button
android:id="@+id/saveBTN"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:background="@color/gray"
android:onClick="saveBTN"
android:text="Save"
android:textColor="@color/white" />
<DatePicker
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/saveBTN"
/>
<TimePicker
android:timePickerMode="spinner"
android:id="@+id/time_picker"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/date_picker"
/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
使用CustomTimePickerDialog代替TimePickerDialog。我认为您的问题将得到解决。
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<String> 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();
}
}
}
答案 1 :(得分:0)
我找到了没有DialogFragment的方法。我添加了一个新函数,并在初始化TimePicker之后对其进行了调用。请参见下面的代码以获取详细信息。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker simpleDatePicker = (DatePicker)findViewById(R.id.date_picker); // initiate a date picker
//Above API 26
// ????
simpleDatePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Do something
}
});
TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.time_picker); // initiate a time picker
simpleTimePicker.setIs24HourView(true);// set to 24 hour
setTimePickerInterval(simpleTimePicker);
simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Do somthing
}
});
}
private void setTimePickerInterval(TimePicker timePicker) {
try {
int TIME_PICKER_INTERVAL = 5;
NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
"minute", "id", "android"));
minutePicker.setMinValue(0);
minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
}
}
public void cancelBTN(View view) {
}
public void saveBTN(View view) {
}
}