日期选择器 DatePicker 的使用方法
2016-05-04 15:33
811 查看
收集了别人的代码 自己再添加另外过滤日期范围的部分
1. 调用代码 (布局代码在下面)
2.布局代码 dialog.xml
1. 调用代码 (布局代码在下面)
/** * 弹出日期选择框 * @param view */ public void btn1(View view) { LayoutInflater l = LayoutInflater.from(this); View v = l.inflate(R.layout.dialog, null); final DatePicker datePicker = (DatePicker) v.findViewById(R.id.datepicker); datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (isDateAfter(view)) { view.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), this); Toast.makeText(MainActivity.this, "超出范围-今天", Toast.LENGTH_SHORT).show(); } if (isDateBefore(view)) { view.init(2010, 0, 1, this); Toast.makeText(MainActivity.this, "超出范围-最低范围", Toast.LENGTH_SHORT).show(); } } //超出当月 private boolean isDateAfter(DatePicker tempView) { if (tempView.getYear() > c.get(Calendar.YEAR)) { return true; } else if (tempView.getYear() == c.get(Calendar.YEAR)) { if (tempView.getMonth() > c.get(Calendar.MONTH)) { return true; } else if (tempView.getMonth() == c.get(Calendar.MONTH)) { if (tempView.getDayOfMonth() > c.get(Calendar.DATE)) { return true; } else { return false; } } else { return false; } } else { return false; } } //小于最低范围 private boolean isDateBefore(DatePicker tempView) { if (tempView.getYear() < 2010) { return true; } else return false; } }); Dialog dialog = new AlertDialog.Builder(this) .setTitle( datePicker.getYear() + "年" + (datePicker.getMonth() + 1) + "月" + datePicker.getDayOfMonth() + "日") .setView(v) .setIcon(R.drawable.ic_launcher) .setNeutralButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, datePicker.getYear() + "年" + (datePicker.getMonth() + 1) + "月" + datePicker.getDayOfMonth() + "日", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); dialog.show(); }
2.布局代码 dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datepicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- EditText特殊用法
- 第八课等于之集合对象的操作——C#计算器编程教学
- php 操作redis
- Curator 异步获取结果
- 【框架】CoordinatorLayout协调者布局
- js中的call和apply方法的区别
- HDU 2066 一个人的旅行
- C语言malloc的使用浅谈
- DevExpress控件-searchControl使用方法(图文)
- load data infile 补充
- lvs 持久连接 80 和443 端口iptables mangle
- 记录利用ettercap进行简单的arp欺骗和mitm攻击过程
- Linux——简单介绍
- nrf51822 --- 外部中断(按键2)
- Android6.0权限申请模型
- 浏览器的userAgent识别
- 在Visual Studio Code中配置GO开发环境
- 项目开发模型
- C#基础知识六之委托(delegate、Action、Func、predicate)
- Java线程