来源:http://gundumw100.iteye.com/blog/1407278
系统自带的DatePickerDialog只能一次性选择一个日期,有时候我们需要这样的功能:需要录入2个日期,比如开始日期和结束日期,如果采用DatePickerDialog需要弹出2次Dialog,能不能一次就搞定呢?那就需要下面的内容了。
自定义一个DatePickerDialog,实现OnDateChangedListener接口,里面有2个DatePicker,然后分别对这2个DatePicker设置日期就行了。
package qianlong.qlmobile.view;
import qianlong.qlmobile.tablet.yinhe.hk.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
public class DatePickerDialog extends Dialog implements OnDateChangedListener{
private Context context;
private Button apply,cancel;
private DatePicker startDatePicker,endDatePicker;
private int start_year;
private int start_monthOfYear;
private int start_dayOfMonth;
private int end_year;
private int end_monthOfYear;
private int end_dayOfMonth;
public DatePickerDialog(Context context,
int start_year,
int start_monthOfYear,
int start_dayOfMonth,
int end_year,
int end_monthOfYear,
int end_dayOfMonth
) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
this.start_year=start_year;
this.start_monthOfYear=start_monthOfYear;
this.start_dayOfMonth=start_dayOfMonth;
this.end_year=end_year;
this.end_monthOfYear=end_monthOfYear;
this.end_dayOfMonth=end_dayOfMonth;
init();
}
public DatePickerDialog(Context context,int theme,
int start_year,
int start_monthOfYear,
int start_dayOfMonth,
int end_year,
int end_monthOfYear,
int end_dayOfMonth
) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
this.start_year=start_year;
this.start_monthOfYear=start_monthOfYear;
this.start_dayOfMonth=start_dayOfMonth;
this.end_year=end_year;
this.end_monthOfYear=end_monthOfYear;
this.end_dayOfMonth=end_dayOfMonth;
init();
}
private void init(){
// this.setCanceledOnTouchOutside(true);
this.setCancelable(true);
this.setTitle("日期选择器");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ipo_dialog_date_picker);
initViews();
initValues();
}
private void initViews(){
apply=(Button)findViewById(R.id.apply);
apply.setOnClickListener(clickListener);
cancel=(Button)findViewById(R.id.cancel);
cancel.setOnClickListener(clickListener);
startDatePicker=(DatePicker)findViewById(R.id.startDatePicker);
endDatePicker=(DatePicker)findViewById(R.id.endDatePicker);
}
private void initValues(){
startDatePicker.init(start_year, start_monthOfYear, start_dayOfMonth, this);
endDatePicker.init(end_year, end_monthOfYear, end_dayOfMonth, this);
}
private Button.OnClickListener clickListener=new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.apply:
if(onDateSetListener!=null){
onDateSetListener.onDateSet(
start_year, start_monthOfYear, start_dayOfMonth,
end_year, end_monthOfYear, end_dayOfMonth
);
}
dismiss();
break;
case R.id.cancel:
dismiss();
break;
default:
break;
}
}
};
private OnDateSetListener onDateSetListener;
public interface OnDateSetListener {
void onDateSet(
int start_year,
int start_monthOfYear,
int start_dayOfMonth,
int end_year,
int end_monthOfYear,
int end_dayOfMonth
);
}
public void setOnDateSetListener(OnDateSetListener onDateSetListener){
this.onDateSetListener=onDateSetListener;
}
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
if(view==startDatePicker){
this.start_year=year;
this.start_monthOfYear=monthOfYear;
this.start_dayOfMonth=dayOfMonth;
}else if(view==endDatePicker){
this.end_year=year;
this.end_monthOfYear=monthOfYear;
this.end_dayOfMonth=dayOfMonth;
}
// updateDate(view,year,monthOfYear,dayOfMonth);
}
// public void updateDate(
// DatePicker view, int year, int monthOfYear,int dayOfMonth){
// view.updateDate(year, monthOfYear, dayOfMonth);
// }
}
我只是简单的实现了一下,更多的扩展请大家添上。
ipo_dialog_date_picker.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始时间:"
android:textSize="@dimen/font_middler"
android:textColor="@color/white"
android:gravity="center"
/>
<DatePicker
android:id="@+id/startDatePicker"
android:padding="5dip"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="5dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="结束时间:"
android:textSize="@dimen/font_middler"
android:textColor="@color/white"
android:gravity="center"
/>
<DatePicker
android:id="@+id/endDatePicker"
android:padding="5dip"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="20dip"
>
<Button android:id="@+id/apply"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="确认"
android:textSize="@dimen/font_middler"
/>
<Button android:id="@+id/cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="取消"
android:textSize="@dimen/font_middler"
android:layout_marginLeft="20dip"
/>
</LinearLayout>
</LinearLayout>
用法:
private Calendar calendar_start = Calendar.getInstance();
private Calendar calendar_end = Calendar.getInstance();
//在需要的地方填入如下代码(通常是在Button的click事件中.)
qianlong.qlmobile.view.DatePickerDialog d=new qianlong.qlmobile.view.DatePickerDialog(
HK_TradeIPOActivity.this.getParent(),
calendar_start.get(Calendar.YEAR),
calendar_start.get(Calendar.MONTH),
calendar_start.get(Calendar.DAY_OF_MONTH),
calendar_end.get(Calendar.YEAR),
calendar_end.get(Calendar.MONTH),
calendar_end.get(Calendar.DAY_OF_MONTH)
);
d.setOnDateSetListener(onDateSetListener);
d.show();
//自己实现需要的功能
private qianlong.qlmobile.view.DatePickerDialog.OnDateSetListener onDateSetListener=new qianlong.qlmobile.view.DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(int start_year,
int start_monthOfYear,
int start_dayOfMonth,
int end_year,
int end_monthOfYear,
int end_dayOfMonth) {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
calendar_start.set(Calendar.YEAR, start_year);
calendar_start.set(Calendar.MONTH, start_monthOfYear);
calendar_start.set(Calendar.DAY_OF_MONTH, start_dayOfMonth);
calendar_end.set(Calendar.YEAR, end_year);
calendar_end.set(Calendar.MONTH, end_monthOfYear);
calendar_end.set(Calendar.DAY_OF_MONTH, end_dayOfMonth);
String startDate=simpleDateFormat.format(calendar_start.getTime());
String endDate=simpleDateFormat.format(calendar_end.getTime());
//doSomething(startDate,endDate)
}
};
Android开发之日历控件实现
http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2418178.html