天天看點

自定義Dialog:雙日期選擇器

系統自帶的DatePickerDialog隻能一次性選擇一個日期,有時候我們需要這樣的功能:需要錄入2個日期,比如開始日期和結束日期,如果采用DatePickerDialog需要彈出2次Dialog,能不能一次就搞定呢?那就需要下面的内容了。

自定義一個DatePickerDialog,實作OnDateChangedListener接口,裡面有2個DatePicker,然後分别對這2個DatePicker設定日期就行了。

自定義Dialog:雙日期選擇器
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)	
        	
            
		}
		
	};

           

over.

PS:iteye的部落格做的越來越爛了,可能以後不在這裡混了!

Android開發之月曆控件實作

http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2418178.html

  • 自定義Dialog:雙日期選擇器
  • 大小: 75.4 KB
  • 檢視圖檔附件