Android 1.5 中 onDateChanged() 中具有最大和最小日期的日期选择器?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样,我解决了我的问题
This way, i solved my problem