gpt4 book ai didi

java - DatePickerDialog 将选项限制为最长一周

转载 作者:行者123 更新时间:2023-12-01 12:28:51 25 4
gpt4 key购买 nike

我已将 DatePickerDialog 包含到我的 Activity 中,它允许用户选择日历月份和日期。但是,我想将选择限制为从现在起一周以内(当前日期之前没有选项)。

我已经研究过setMinDate(long minDate) 和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(日历.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(

最佳答案

这可能会帮助你...试试这个代码...

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;
}

关于java - DatePickerDialog 将选项限制为最长一周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26113167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com