Android 1.5中onDateChanged()的最大和最小日期的日期选择器?

时间:2011-12-30 03:53:32

标签: android-datepicker

我正在使用android 1.5中的DatePicker。我试图设置最大和最小日期。最小日期应该是当前日期,最大日期应该是我将从字符串提供的日期为maxYear,maxMonth,maxDay。(假设今天日期= 2011年12月30日,但它选择29/12/2011)

一切工作正常,最小日期。因为它只显示当前日期。但是在选择器上使用减号按钮进行选择时,它会选择一个小于今天日期的日期。 在最大日期选择的情况下,它选择的日期,月份和年份超过最大日期。

如何限制用户不要选择少于最小日期和最长日期。我必须提出什么额外的条件才能让它变得完美?

enter code here

///Whole code same


mport java.sql.Date;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.DatePicker.OnDateChangedListener;

 public class DatePickerActivity extends Activity implements Button.OnClickListener {
 public String dateOutput=null;

final Calendar c = Calendar.getInstance();
int minYear1 = c.get(Calendar.YEAR); 
int minMonth1 = c.get(Calendar.MONTH);
int minDay1 = c.get(Calendar.DAY_OF_MONTH);

int maxYear = 2011;// 
int maxMonth = 12;
int maxDay = 29;

int minYear = minYear1;
int minMonth = minMonth1;
int minDay = minDay1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker DateDP = (DatePicker) findViewById (R.id.ad_date_picker);
DateDP.init(minYear1, minMonth1, minDay1, new OnDateChangedListener()
  {

    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
             if (year < minYear)
             view.updateDate(minYear, minMonth, minDay);

             if (monthOfYear < minMonth && year == minYear  )
             view.updateDate(minYear, minMonth, minDay );



             if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
             view.updateDate(minYear, minMonth, minDay);


             if (year > maxYear)
             view.updateDate(maxYear, maxMonth, maxDay);

             if (monthOfYear > maxMonth && year == maxYear)
             view.updateDate(maxYear, maxMonth, maxDay);

             if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
             view.updateDate(maxYear, maxMonth, maxDay);

            dateOutput = String.format("Date Selected: %02d/%02d/%04d", 
                                        dayOfMonth, monthOfYear+1, year);
            // Log.d("Debug", dateOutput);



            Toast.makeText(DatePickerActivity.this,dateOutput,   Toast.LENGTH_SHORT).show();


        }}); // DateDP.init()



    }



 }

1 个答案:

答案 0 :(得分:4)

这样,我解决了我的问题

DateDP.init(minYear, minMonth, minDay, new OnDateChangedListener()
    {

    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    { 
            if (year > maxYear ||monthOfYear > maxMonth && year == maxYear||
                 dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth){
            // Toast.makeText(DatePickerActivity.this,"max year", Toast.LENGTH_SHORT).show();

            view.updateDate(maxYear, maxMonth, maxDay);

            dateOutput = String.format("%04d-%02d-%02d", 
                 maxYear, maxMonth+1, maxDay);
           //Toast.makeText(DatePickerActivity.this,"maxYear  "+dateOutput, Toast.LENGTH_SHORT).show();

            }else if(year < minYear ||monthOfYear < minMonth && year == minYear||
                 dayOfMonth < minDay && year == minYear && monthOfYear == minMonth){


            //Toast.makeText(DatePickerActivity.this,"min year", Toast.LENGTH_SHORT).show();

             view.updateDate(minYear, minMonth, minDay );

            dateOutput = String.format("%04d-%02d-%02d", 
                    minYear, minMonth+1, minDay);

                 //Toast.makeText(ManageShowing.this,dateOutput, Toast.LENGTH_SHORT).show();                              

            }else{

             //Toast.makeText(ManageShowing.this,"else", Toast.LENGTH_SHORT).show();

             dateOutput = String.format("%04d-%02d-%02d", 
                     year, monthOfYear+1, dayOfMonth);

                    // Toast.makeText(ManageShowing.this,dateOutput, Toast.LENGTH_SHORT).show();                     

            }
            mDateDisplay.setText(dateOutput);

    }}); // DateDP.init()