Android 如何实现时间年月日三级联动呢 直接上代码 看效果图

it2024-12-06  20

对于Android 初学者来说我推荐 :TimePickerView 好用 代码简洁更容易看懂 优点: 1 , 年月日可自行修改 2 ,字体颜色 展示都可修改。直接上代码 示例如下:

步骤 1.添加Jcenter仓库 Gradle依赖: implementation 'com.contrarywind:Android-PickerView:3.2.4'

2 ,

public static void showDatePickerStart(Activity activity, TextView textView) { Calendar startDate = Calendar.getInstance(); startDate.set(2020, 00, 01); Calendar endDate = Calendar.getInstance(); showDatePicker(activity, startDate, endDate, calendar -> textView.setText(calendar.get(Calendar.YEAR) + "年" + (String.format("%02d", calendar.get(Calendar.MONTH) + 1)) + "月" + (String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH)) + "日"))); } public static void showDatePicker(Context context, Calendar startDate, Calendar endDate, Calendar selectDate, final DatePickerCallback callback) { TimePickerView.Builder builder = new TimePickerView.Builder(context, (date, v) -> { if (callback != null) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); callback.onDateSelect(calendar); } }); builder.setSubmitText("确认"); builder.setCancelText("取消"); builder.setType(TimePickerView.Type.YEAR_MONTH_DAY); builder.setLabel("年", "月", "日", "", "", ""); builder.isCenterLabel(false); builder.isDialog(false); builder.setBgColor(0xFFFFFFFF); builder.setSubmitColor(0xFF29346C); builder.setCancelColor(0xFF29346C); //builder.setDividerColor(0xFFFB2C3C); builder.setContentSize(15); builder.setLineSpacingMultiplier(3.5f); builder.setRangDate(startDate, endDate); TimePickerView pickerView = builder.build(); pickerView.setDate(selectDate); pickerView.show(); }

是不是很简单呢? 觉的不错的小伙伴可以点个赞。

 

 

 

 

最新回复(0)