在api 11之前在android中将日期设置为min和max

时间:2014-07-08 08:06:10

标签: android dialog datepicker

我无法将日期纠察对话框设置为最小和最大日期。 用户可以无限向上和向后滚动以选择任何年份或日期。但我想使用受限制的datepicker对话框。 任何人都可以解雇我。

提前谢谢。

3 个答案:

答案 0 :(得分:1)

试过并测试过!

<强> DatePickerDialogWithMaxMinRange.java

public class DatePickerDialogWithMaxMinRange extends DatePickerDialog {

static int maxYear = 2005;
static int maxMonth = 11;
static int maxDay = 31;

int minYear = 1955;
int minMonth = 0;
int minDay = 1;

public DatePickerDialogWithMaxMinRange(Context context,
        OnDateSetListener callBack, int minYear, int minMonth, int minDay,
        int maxYear, int maxMonth, int maxDay) {
    super(context, callBack, maxYear, maxMonth, maxDay);
    this.minDay = minDay;
    this.minMonth = minMonth;
    this.minYear = minYear;
    DatePickerDialogWithMaxMinRange.maxDay = maxDay;
    DatePickerDialogWithMaxMinRange.maxMonth = maxMonth;
    DatePickerDialogWithMaxMinRange.maxYear = maxYear;
}

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    super.onDateChanged(view, year, monthOfYear, dayOfMonth);

    if (year > maxYear || monthOfYear > maxMonth && year == maxYear
            || dayOfMonth > maxDay && year == maxYear
            && monthOfYear == maxMonth) {
        view.updateDate(maxYear, maxMonth, maxDay);
    } else if (year < minYear || monthOfYear < minMonth && year == minYear
            || dayOfMonth < minDay && year == minYear
            && monthOfYear == minMonth) {
        view.updateDate(minYear, minMonth, minDay);
    }
}
}

<强> MyAndroidAppActivity.java

public class MyAndroidAppActivity extends Activity {


DatePickerDialogWithMaxMinRange datePickerDialog= null;
DatePickerDialog.OnDateSetListener datePickerOnDateSetListener;
Calendar myCalendar;

private TextView tvDisplayDate;
private Button btnChangeDate;

static final int DATE_DIALOG_ID = 999;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tvDisplayDate = (TextView)findViewById(R.id.lblDate);
    setDate();
    addListenerOnButton();

}

public void addListenerOnButton() {

    btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

    btnChangeDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            datePickerDialog.show();

        }

    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date

        return new DatePickerDialog(this, datePickerOnDateSetListener, intCurrentYear, intCurrentMonth,intCurrentDay);
    }
    return null;
}

private int intCurrentYear;
private int intCurrentMonth;
private int intCurrentDay;
private int intMaxYear;
private int intMaxMonth;
private int intMaxDay;
private int intMinYear;
private int intMinMonth;
private int intMinDay;


public void setDate() {
    /*
     * Initialise Listener for date set
     */

    datePickerOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            tvDisplayDate.setText(new StringBuilder().append(year)
                    .append("-").append(monthOfYear + 1).append("-")
                    .append(dayOfMonth));
        }
    };

    // initialise DatePicker 

    myCalendar = Calendar.getInstance();

    intCurrentYear = myCalendar.get(Calendar.YEAR);
    intCurrentMonth = myCalendar.get(Calendar.MONTH);
    intCurrentDay = myCalendar.get(Calendar.DAY_OF_MONTH);


    intMaxYear =  intCurrentYear;
    intMaxMonth = intCurrentMonth;
    intMaxDay =  intCurrentDay;

    intMinYear =   2011;
    intMinMonth = intCurrentMonth;
    intMinDay =  intCurrentDay; 

    datePickerDialog = new DatePickerDialogWithMaxMinRange(MyAndroidAppActivity.this, datePickerOnDateSetListener,intMinYear,intMinMonth,intMinDay,intMaxYear,intMaxMonth,intMaxDay);
}

main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

答案 1 :(得分:0)

检查以下代码,了解限制为特定限制的自定义日期选择器对话框。

<强> DatePickerRestriction.java: -

package com.anil.android;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class DatePickerRestriction extends Activity {

 private TextView tvDisplayDate;
 private DatePicker dpResult;    

 private int mYear;
 private int mMonth;
 private int mDay;
 static final int DATE_DIALOG_ID = 1;

 //these variables are used to set max years need to set on Date Picker dialog...
 int minYear1 = 2000; 
 int minMonth1 = 0;//0-january , 1-february , 2-march..
 int minDay1 =1;

 int minYear = minYear1;
 int minMonth = minMonth1;
 int minDay = minDay1;

 //these are the minimum dates to set Datepicker..
 private int year;
 private int month;
 private int day;    

 public String dateOutput=null;


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH); 

        setCurrentDateOnView();

    Button pickDate = (Button) findViewById(R.id.btnChangeDate);
    pickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
            }
    });

 }

 // display current date
 public void setCurrentDateOnView() {

    System.out.println("----------setCurrentDateOnView()-----------");

    tvDisplayDate = (TextView) findViewById(R.id.tvDate);
    dpResult = (DatePicker) findViewById(R.id.dpResult);

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    tvDisplayDate.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(month + 1).append("-").append(day).append("-")
            .append(year).append(" "));

    // set current date into datepicker
    dpResult.init(year, month, day, null);
 }


 //this was the main part in program to Restrict the DatePicker Dialog  to only its current date.
 @Override
 protected Dialog onCreateDialog(int id) {
    System.out.println("----------onCreateDialog()-----------");
    DatePickerDialog _date = null;

    switch (id) {
    case DATE_DIALOG_ID:
            _date =  new DatePickerDialog(this,  datePickerListener,
                    year, mMonth, mDay){
       @Override
       public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
       {   
           System.out.println("----------onDateChanged()-----------"+mYear+"--"+year);
           System.out.println("----------onDateChanged()-----------"+mMonth+"--"+monthOfYear);
           System.out.println("----------onDateChanged()-----------"+mDay+"--"+dayOfMonth);


          /* These lines of commented code used for only setting the maximum date on Date Picker.. 
           * 
           * if (year > mYear && year)
               view.updateDate(mYear, mMonth, mDay);

               if (monthOfYear > mMonth && year == mYear )
               view.updateDate(mYear, mMonth, mDay);

               if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
               view.updateDate(mYear, mMonth, mDay);*/


           //these below lines of code used for setting the maximum as well as minimum dates on Date Picker Dialog..
           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 > mYear)
               view.updateDate(mYear, mMonth, mDay);

               if (monthOfYear > mMonth && year == mYear)
               view.updateDate(mYear, mMonth, mDay);

               if (dayOfMonth > mDay && year == mYear && monthOfYear == mMonth)
               view.updateDate(mYear, mMonth, mDay);

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

              Toast.makeText(DatePickerRestriction.this,dateOutput,   Toast.LENGTH_SHORT).show();
       }
   };      
    }
     return _date;
  }


 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {            

        System.out.println("----------onDateSet()-----------");                                

        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        tvDisplayDate.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));

        // set selected date into datepicker also
        dpResult.init(year, month, day, null);
    }
 };

}

<强> main.xml中: -          

<Button
    android:id="@+id/btnChangeDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Date" />

<TextView
    android:id="@+id/lblDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Current Date (M-D-YYYY): "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/tvDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

<DatePicker
    android:id="@+id/dpResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

答案 2 :(得分:0)

我没有使用过DatePicker,但文档没有显示任何可以限制Picker显示的最大日期的方法,但仍然可以检查它返回的日期,并且可以通知用户有关验证标准。