效果图如下:
具体步骤如下:
1.在app的gradle里添加依赖:
//引进calendarview
compile 'com.prolificinteractive:material-calendarview:1.4.3'
2.自定义新的TimePicker:
public class TimePickerView extends TimePicker {
public TimePickerView(Context context) {
super(context);
}
public TimePickerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TimePickerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//重写如下方法即可:(应该也适用于其他控件)
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}
3.自定义TimePicker其外的Linearlayout,并重写其外的Linearlayout的onInterceptTouchEvent:
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
//请求其Parents不对Touch进行拦截
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
//给LinearLayout加入Touch事件,使得所有Layout区域的Touch操作均不受拦截
public boolean onTouchEvent(MotionEvent ev)
{
return true;
}
}
4.xml布局:
<com.lotus.calendardemo.CustomLinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 显示用户输入日期、时间的控件 -->
<EditText android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 定义一个TimePicker组件 -->
<com.lotus.calendardemo.TimePickerView
android:id="@+id/timePicker"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center_horizontal" />
<!-- 定义一个DatePicker组件 -->
<DatePicker
android:id="@+id/datePicker"
android:layout_width="350dp"
android:layout_height="450dp"
android:layout_gravity="center_horizontal"
android:startYear="1000"
android:endYear="3000"
android:calendarViewShown="true"
android:spinnersShown="true" />
</LinearLayout>
</ScrollView>
</com.lotus.calendardemo.CustomLinearLayout>
5.由于Android自定义了DatePicker、TimePicker的大小,所以我通过调节app字体大小的方法来帮助缩小控件:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryDark</item>
<item name="colorPrimaryDark">@color/colorAccent</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:textSize">18sp</item>
</style>
6.activity代码:
// 定义5个记录当前时间的变量
private int year;
private int month;
private int day;
private int hour;
private int minute;
DatePicker datePicker;
TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//使用日期、时间选择器(DatePicker和TimePicker)
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
// 获取当前的年、月、日、小时、分钟
Calendar calendar1 = Calendar.getInstance();
year = calendar1.get(Calendar.YEAR);
month = calendar1.get(Calendar.MONTH);
day = calendar1.get(Calendar.DAY_OF_MONTH);
hour = calendar1.get(Calendar.HOUR);
minute = calendar1.get(Calendar.MINUTE);
// 初始化DatePicker组件,初始化时指定监听器
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker arg0, int year, int month, int day)
{
HomeActivity.this.year = year;
HomeActivity.this.month = month;
HomeActivity.this.day = day;
// 显示当前日期、时间
showDate(year, month, day, hour, minute);
}
});
// 为TimePicker指定监听器
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
HomeActivity.this.hour = hourOfDay;
HomeActivity.this.minute = minute;
// 显示当前日期、时间
showDate(year, month, day, hour, minute);
}
});
}
// 定义在EditText中显示当前日期、时间的方法
private void showDate(int year, int month, int day, int hour, int minute)
{
EditText show = (EditText) findViewById(R.id.show);
show.setText( year + "年" + (month + 1) + "月" + day + "日 " + hour + "时" + minute + "分");
}
注:
参考了一些大神的资料,觉得很有用:
1.TimePicker在ScrollView中滑动冲突的解决方法(更新适用于全控件的方法) - checkin001的专栏 - CSDN博客 http://blog.csdn.net/checkin001/article/details/8298313
2.Android开发之日期、时间选择器(DatePicker和TimePicker)的功能和用法 - fengyuzhengfan的专栏 - CSDN博客 http://blog.csdn.net/fengyuzhengfan/article/details/38237065
3.Android调整TimePicker和DatePicker大小 - Barving的专栏 - CSDN博客 http://blog.csdn.net/u011819464/article/details/41012347
4.两分钟彻底让你明白Android中onInterceptTouchEvent与onTouchEvent(图文)! - Android_Tutor的专栏 - CSDN博客 http://blog.csdn.net/android_tutor/article/details/7193090