Android : 是否可以在时间选择器中只显示季度? 00:15:30:45
Android : Is It Possible To Show Only Quaters in Time Picker ? 00 : 15 : 30 :45
我只需要在时间选择器中显示 [00 , 15 , 30 ,45]
我搜索过但找不到任何东西,有人知道如何实现吗?
您可以通过创建自定义圆形 drawable 来创建自定义时间选择器,并将其作为 framelayout 的背景,并获取时间值监听 textviews 的点击事件
代码:
bg_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="2dp"/>
</shape>
layout_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/bg_circle"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:layout_gravity="center_vertical|end"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:layout_gravity="center_horizontal|bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="45"
android:layout_gravity="center_vertical|start"
/>
</FrameLayout>
希望对您有所帮助
Android 时间选择器不提供 setInterval()
方法,我们需要使用 MaterialDateTimepicker
implementation 'com.wdullaer:materialdatetimepicker:3.6.3'
它的最新版本与我的一些库冲突,这就是我使用 3.6.3 的原因
还有那些正在使用 NumberPicker
的人这样做对我来说不起作用
Calendar now = Calendar.getInstance();
TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(mTimePickerListener,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
timePickerDialog.setThemeDark(false);
timePickerDialog.setTitle("TimePicker Title");
timePickerDialog.setTimeInterval(1, 15, 30); // 15 Minutes Interval
//timePickerDialog.setTimeInterval(1, 30, 60); // 30 Minutes Interval
timePickerDialog.setAccentColor(Color.parseColor("#9C27B0"));
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
timePickerDialog.show(getFragmentManager(), "Timepickerdialog");
}
private TimePickerDialog.OnTimeSetListener mTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
}
};
我只需要在时间选择器中显示 [00 , 15 , 30 ,45]
我搜索过但找不到任何东西,有人知道如何实现吗?
您可以通过创建自定义圆形 drawable 来创建自定义时间选择器,并将其作为 framelayout 的背景,并获取时间值监听 textviews 的点击事件
代码: bg_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="2dp"/>
</shape>
layout_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/bg_circle"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:layout_gravity="center_vertical|end"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:layout_gravity="center_horizontal|bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="45"
android:layout_gravity="center_vertical|start"
/>
</FrameLayout>
希望对您有所帮助
Android 时间选择器不提供 setInterval()
方法,我们需要使用 MaterialDateTimepicker
implementation 'com.wdullaer:materialdatetimepicker:3.6.3'
它的最新版本与我的一些库冲突,这就是我使用 3.6.3 的原因
还有那些正在使用 NumberPicker
的人这样做对我来说不起作用
Calendar now = Calendar.getInstance();
TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(mTimePickerListener,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
timePickerDialog.setThemeDark(false);
timePickerDialog.setTitle("TimePicker Title");
timePickerDialog.setTimeInterval(1, 15, 30); // 15 Minutes Interval
//timePickerDialog.setTimeInterval(1, 30, 60); // 30 Minutes Interval
timePickerDialog.setAccentColor(Color.parseColor("#9C27B0"));
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
timePickerDialog.show(getFragmentManager(), "Timepickerdialog");
}
private TimePickerDialog.OnTimeSetListener mTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
}
};