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

发布于 2024-12-23 13:29:09 字数 2645 浏览 0 评论 0原文

我正在 android 1.5 中开发 DatePicker。我正在尝试设置最大和最小日期。最小日期应该是当前日期,最大日期应该是我将从字符串中提供的日期,如 maxYear、maxMonth、maxDay。(假设今天的日期=30/12/2011,但它选择 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()



    }



 }

I am working on DatePicker in android 1.5. I am trying to set Maximum and minimum date.Minimum date should be the current date and maximum date should be the date which I will supply from string as maxYear, maxMonth, maxDay.(Suppose Todays date=30/12/2011, but it selects 29/12/2011)

Everything working fine with minimum date.As it shows current date only.But on selection using minus button on picker, it selects a date less than today's date.
While in case of maximum date selection it selects day, month and year more than the maximum date.

How to restrict user not to select less than minimum date and maximum date. What extra condition I have to put to make it perfect?

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()



    }



 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2024-12-30 13:29:09

这样,我解决了我的问题

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()

This way, i solved my problem

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文