Android 自定义带两个DatePicker的DatePickerDialog
16lz
2021-01-23
今天介绍一下自定义带两个DatePicker的Dialog,首先xml布局/res/layout/custom_date_picker.xml:
代码:
// These variables will hold the date values laterprivate int startYear, startMonth, startDay, endYear, endMonth, endDay;/** * Displays the start and end date picker dialog */public void showDatePicker() { // Inflate your custom layout containing 2 DatePickers LayoutInflater inflater = (LayoutInflater) getLayoutInflater(); View customView = inflater.inflate(R.layout.custom_date_picker, null); // Define your date pickers final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate); final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate); // Build the dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(customView); // Set the view of the dialog to your custom layout builder.setTitle("Select start and end date"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { startYear = dpStartDate.getYear(); startMonth = dpStartDate.getMonth(); startDay = dpStartDate.getDayOfMonth(); endYear = dpEndDate.getYear(); endMonth = dpEndDate.getMonth(); endDay = dpEndDate.getDayOfMonth(); dialog.dismiss(); }}); // Create and show the dialog builder.create().show();}
效果图:
更多相关文章
- android 两个 activity 用 handler传递数据
- android 添加快捷方式 启动两个应用
- 两个小时精通Android开发之按键映射篇
- 研究一下Android滑屏的功能的原理,及scrollTo和scrollBy两个方法
- Android 开发获取通知栏权限时会出现两个应用图标
- Android 拼接两个图片
- 使用viewpager切换fragment加载两个webview时卡顿
- 今天和大家讲一下怎么样简单的把壁纸作为背景,在中间加一个textvi
- Android获取两个日期之间间隔的天数