DatePicker、TimePicker基本用法
阿新 • • 發佈:2019-01-25
前言
在時間和日期開發中,TimePicker和DatePicker經常使用到,這倆個控制元件是嵌入到View中的,如果想使用彈窗式的,可是使用TimePickerDialog和DataPickerDialog。這次我主要是實現了一個帶確定和取消按鈕的時間選擇器,為的是以後可以直接使用,不用再多寫程式碼了。
佈局
layout_timepicker.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height ="match_parent" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="16sp"
/>
<TimePicker
android:id="@+id/timepicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
layout_datepicker.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇日期"
android:textSize="16sp"
android:layout_alignParentTop="true"
/>
<DatePicker
android:id="@+id/datepicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:layout_below="@+id/tv_title"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Factory
TimeFactory:
Builder mDialog;
TimePicker mTimePicker;
TextView tv_title;
TimeFactoryListener listener;
int mHour;
int mMinute;
static TimeFactory mTimeFactory;
public static TimeFactory initFactory()
{
if(mTimeFactory == null)
{
mTimeFactory = new TimeFactory();
}
return mTimeFactory;
}
public void setFactoryListener(TimeFactoryListener listener)
{
this.listener = listener;
}
public Builder createTimePickerDialog(Context context)
{
View view = LayoutInflater.from(context).inflate(R.layout.layout_timepicker, null);
tv_title = (TextView) view.findViewById(R.id.tv_title);
mTimePicker = (TimePicker) view.findViewById(R.id.timepicker);
mDialog = new Builder(context);
mDialog.setView(view);
mTimePicker.setIs24HourView(true);
mHour = mTimePicker.getCurrentHour();
mMinute = mTimePicker.getCurrentMinute();
tv_title.setText(mHour+":"+mMinute);
mDialog.setPositiveButton("確定", onClickListener);
mDialog.setNegativeButton("取消", onClickListener);
mTimePicker.setOnTimeChangedListener(onTimeChangedListener);
return mDialog;
}
final OnTimeChangedListener onTimeChangedListener = new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hour, int minute)
{
tv_title.setText(hour+":"+minute);
mHour = hour;
mMinute = minute;
}
};
final OnClickListener onClickListener = new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
// 確定
case -1:
listener.setTime(mHour, mMinute);
break;
// 取消
case -2:
listener.cancel();
break;
// 忽略
case -3:
break;
default:
break;
}
}
};
public interface TimeFactoryListener
{
public void setTime(int hour,int minute);
public void cancel();
}
DateFactory:
Builder mDialog;
TimePicker mTimePicker;
TextView tv_title;
TimeFactoryListener listener;
int mHour;
int mMinute;
static TimeFactory mTimeFactory;
public static TimeFactory initFactory()
{
if(mTimeFactory == null)
{
mTimeFactory = new TimeFactory();
}
return mTimeFactory;
}
public void setFactoryListener(TimeFactoryListener listener)
{
this.listener = listener;
}
public Builder createTimePickerDialog(Context context)
{
View view = LayoutInflater.from(context).inflate(R.layout.layout_timepicker, null);
tv_title = (TextView) view.findViewById(R.id.tv_title);
mTimePicker = (TimePicker) view.findViewById(R.id.timepicker);
mDialog = new Builder(context);
mDialog.setView(view);
mTimePicker.setIs24HourView(true);
mHour = mTimePicker.getCurrentHour();
mMinute = mTimePicker.getCurrentMinute();
tv_title.setText(mHour+":"+mMinute);
mDialog.setPositiveButton("確定", onClickListener);
mDialog.setNegativeButton("取消", onClickListener);
mTimePicker.setOnTimeChangedListener(onTimeChangedListener);
return mDialog;
}
final OnTimeChangedListener onTimeChangedListener = new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hour, int minute)
{
tv_title.setText(hour+":"+minute);
mHour = hour;
mMinute = minute;
}
};
final OnClickListener onClickListener = new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
// 確定
case -1:
listener.setTime(mHour, mMinute);
break;
// 取消
case -2:
listener.cancel();
break;
// 忽略
case -3:
break;
default:
break;
}
}
};
public interface TimeFactoryListener
{
public void setTime(int hour,int minute);
public void cancel();
}
主要的實現方法就是,在initFactory中例項化工廠,通過createDialog方法建立帶時間選擇器的視窗,在點選視窗按鈕的時間回撥主介面的方法,以達到資料互動的目的。
使用方法
MainActivity:
DateFactory factory = DateFactory.initFactory();
factory.setFactoryListener(new DateFactoryListener()
{
@Override
public void setDate(int year, int monthOfYear, int dayOfMonth)
{
tv_date.setText(year + "-" + (monthOfYear+1) + "-" + dayOfMonth);
}
@Override
public void cancel()
{
}
});
factory.createDatePickerDialog(MainActivity.this).show();
TimeFactory factory = TimeFactory.initFactory();
factory.setFactoryListener(new TimeFactoryListener()
{
@Override
public void setTime(int hour, int minute)
{
tv_date.setText(hour + ":" + minute);
}
@Override
public void cancel()
{
}
});
factory.createTimePickerDialog(MainActivity.this).show();
雖然程式碼看上去比直接使用多了不少,但是以後就可以直接使用Factory類來操作時間了,以後的開發過程省去了不少時間。