我已将DatePickerDialog包含在我的活动中,允许用户选择日历月和日。但是,我想从现在开始限制选择最多一周(并且在当前日期之前没有选项)。
我调查了setMinDate(long minDate) and setMaxDate(long maxDate)
,但在与这些人合作时遇到了困难。
以下是允许用户选择日期和时间的代码
btnCalendar = (Button) findViewById(R.id.btnCalendar);
txtDate = (TextView) findViewById(R.id.txtDate);
txtTime = (TextView) findViewById(R.id.txtTime);
btnCalendar.setOnClickListener(this);
//btnTimePicker.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
非常感谢任何帮助。一切顺利。
更新
btnCalendar = (Button) findViewById(R.id.btnCalendar);
txtDate = (TextView) findViewById(R.id.txtDate);
txtTime = (TextView) findViewById(R.id.txtTime);
btnCalendar.setOnClickListener(this);
//btnTimePicker.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar cal=new GregorianCalendar();
//min date
dpd.getDatePicker().setMinDate(cal.getTime().getTime()-1000);
// max date
cal.add(Calendar.DATE, 10);
Date dte =(Date) cal.getTime();
dpd.getDatePicker().setMaxDate(dte.getTime());
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
上面代码中我遇到错误的两行
//min date
dpd.getDatePicker().setMinDate(cal.getTime().getTime()-1000);
// max date
dpd.getDatePicker().setMaxDate(dte.getTime());
cal.add(Calendar.DATE,7);如果数字小于12,则发生以下错误:
我想做的就是将日历限制为一周
09-30 16:39:07.217: E/AndroidRuntime(2410): FATAL EXCEPTION: main
09-30 16:39:07.217: E/AndroidRuntime(2410): java.lang.NullPointerException
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.CalendarView.onScroll(CalendarView.java:1214)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.CalendarView.access$900(CalendarView.java:76)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.CalendarView$2.onScroll(CalendarView.java:1075)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.ListView.layoutChildren(ListView.java:1762)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.View.layout(View.java:14289)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.Choreographer.doFrame(Choreographer.java:532)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.os.Looper.loop(Looper.java:137)
09-30 16:39:07.217: E/AndroidRuntime(2410): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-30 16:39:07.217: E/AndroidRuntime(2410): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:39:07.217: E/AndroidRuntime(2410): at java.lang.reflect.Method.invoke(Method.java:525)
09-30 16:39:07.217: E/AndroidRuntime(2410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-30 16:39:07.217: E/AndroidRuntime(2410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 16:39:07.217: E/AndroidRuntime(2410): at dalvik.system.NativeStart.main(
答案 0 :(得分:1)
这可以帮助你...... 试试这段代码......
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month,day);
Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+new Date().getTime());
Calendar cal=new GregorianCalendar();
//min date
dialog.getDatePicker().setMinDate(cal.getTime().getTime()-1000);
Log.i("onCreateDialog", "onCreateDialog : MinDate() : "+cal.getTime());
//max date .... added 30 days from currunt date
cal.add(Calendar.DATE, 30);
Date dte =cal.getTime();
dialog.getDatePicker().setMaxDate(dte.getTime());
Log.i("onCreateDialog", "onCreateDialog : MaxDate() : "+dte);
//customizing calendar view
dialog.getDatePicker().setCalendarViewShown(true);
dialog.getDatePicker().setSpinnersShown(false);
dialog.getDatePicker().getCalendarView().setShowWeekNumber(false);
dialog.setCancelable(true);
Log.i("Date", "date : "+System.currentTimeMillis());
return dialog;
}