我想在 DatePickerDialog 中禁用今天日期之前的日期。我是android的新手请建议我怎么做这个。这是我为DatePickerDialog编写的代码。我正在扩展Fragment类。 请帮助我:))
public class OneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_one, container, false);
CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate);
CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate);
CheckInDate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if (v == CheckInDate) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth +1;
day = selectedDay;
// set selected date into textview
CheckInDate.setText(new StringBuilder().append(day)
.append("/").append(month).append("/").append(year)
.append(" "));
}
}, year, month, day);
datePickerDialog.show();
}
}
});
CheckOutDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == CheckOutDate) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
CheckOutDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, year, month, day);
datePickerDialog.show();
}
}
});
return rootView;
}}
答案 0 :(得分:1)
使用DatePicker的setMinDate函数
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();
工作代码在
之下 final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH); // add here +1 if you don't
//want user to select current date also
c.set(Calendar.DAY_OF_MONTH, mDay);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
cal.set(Calendar.YEAR, year);
if(cal.before(c)) {
//toast("Select valid date please.");
// notify user about wrong date.
return;
}
StringBuilder date = new StringBuilder();
//date.delete(0, date.length());
date.append((dayOfMonth<10?"0":"")).append(dayOfMonth)
.append("-").append((monthOfYear + 1) < 10 ? "0" : "")
.append((monthOfYear+1)).append("-").append(year);
//text.setText(date.toString());
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(c.getTimeInMillis());
dpd.show();
希望它能够发挥作用。
答案 1 :(得分:0)
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
year_x = c.get(Calendar.YEAR);
month_x = c.get(Calendar.MONTH);
day_x = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DILOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(getActivity(), datePickerlistner, year_x, month_x,
day_x) {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (year < year_x)
view.updateDate(year_x, month_x, day_x);
if (monthOfYear < month_x && year == year_x)
view.updateDate(year_x, month_x, day_x);
if (dayOfMonth < day_x && year == year_x && monthOfYear == month_x)
view.updateDate(year_x, month_x, day_x);
}
};
return _date;
}
return null;
}
DatePickerDialog.OnDateSetListener datePickerlistner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear;
day_x = dayOfMonth;
btn_date.setText(day_x + "/" + (month_x + 1) + "/" + year_x);
}
};
答案 2 :(得分:0)
Try like this...
DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,mYear, mMonth, mDay);
Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
da.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));
愿它有所帮助!
答案 3 :(得分:0)
在日期集中添加以下内容
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd"); //Format of Date
Date current = dfDate.parse(String.valueOf(year) + "-" +
String.valueOf(month) + "-" + String.valueOf(day));//Converting the current Date
Date selected = dfDate.parse(String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(dayOfMonth)); //Converting the selected date
//然后只需通过以下代码检查
if (selected.before(current)) {
//Do Stuff here
}
答案 4 :(得分:0)
使用此我大约2个月前在StackOverFlow上也发现了这个但是不记得链接所以添加作为答案
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.content.Context;
import android.util.Log;
import android.widget.DatePicker;
public class MyPicker extends DatePickerDialog {
private int maxYear = 0;
private int maxMonth = 0;
private int maxDay = 0;
private int minYear = 0;
private int minMonth = 0;
private int minDay = 0;
private Calendar m_minDateCalendar, m_maxDateCalendar, m_calendar;
private boolean m_fired = false;
public MyPicker(Context context, OnDateSetListener callBack,
Calendar p_minCalendar, Calendar p_maxCalendar,
Calendar p_currentCalendar) {
super(context, callBack, p_currentCalendar.get(Calendar.YEAR),
p_currentCalendar.get(Calendar.MONTH), p_currentCalendar
.get(Calendar.DAY_OF_MONTH));
m_minDateCalendar = (Calendar) p_minCalendar.clone();
m_maxDateCalendar = (Calendar) p_maxCalendar.clone();
m_calendar = (Calendar) p_currentCalendar.clone();
this.minDay = m_minDateCalendar.get(Calendar.DAY_OF_MONTH);
this.minMonth = m_minDateCalendar.get(Calendar.MONTH);
this.minYear = m_minDateCalendar.get(Calendar.YEAR);
this.maxDay = m_maxDateCalendar.get(Calendar.DAY_OF_MONTH);
this.maxMonth = m_maxDateCalendar.get(Calendar.MONTH);
this.maxYear = m_maxDateCalendar.get(Calendar.YEAR);
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
m_fired = false;
this.getDatePicker().setMaxDate(
m_maxDateCalendar.getTime().getTime());
this.getDatePicker().setMinDate(
m_minDateCalendar.getTime().getTime());
} else {
m_fired = true;
}
} catch (Throwable p_e) {
Log.d("m_maxDateCalendar", ""
+ m_maxDateCalendar.getTime().getTime());
Log.d("m_minDateCalendar", ""
+ m_minDateCalendar.getTime().getTime());
Log.d("m_calendar", "" + m_calendar.getTime().getTime());
p_e.printStackTrace();
}
}
@Override
public void onDateChanged(DatePicker p_view, int p_year, int p_monthOfYear,
int p_dayOfMonth) {
super.onDateChanged(p_view, p_year, p_monthOfYear, p_dayOfMonth);
if (m_fired) {
m_calendar.set(Calendar.YEAR, p_year);
m_calendar.set(Calendar.MONTH, p_monthOfYear);
m_calendar.set(Calendar.DAY_OF_MONTH, p_dayOfMonth);
if (m_calendar.after(m_maxDateCalendar)) {
m_fired = true;
if (maxMonth > 11) {
maxMonth = 11;
}
p_view.updateDate(maxYear, maxMonth, maxDay);
} else if (m_calendar.before(m_minDateCalendar)) {
m_fired = true;
if (minMonth > 11) {
minMonth = 11;
}
p_view.updateDate(minYear, minMonth, minDay);
} else {
m_fired = true;
p_view.updateDate(p_year, p_monthOfYear, p_dayOfMonth);
}
}
}
}
您的活动中的使用此方法
private void openDatePicker(int month, int day, int year) {
try {
Log.e("calender", "MONTH = " + month);
Log.e("calender", "DAY_OF_WEEK = " + day);
Log.e("calender", "YEAR = " + year);
m_calendar.set(Calendar.DAY_OF_MONTH, month);
m_calendar.set(Calendar.MONTH, day);
m_calendar.set(Calendar.YEAR, year);
int p_minYear, p_minMonth, p_minDay;
int p_maxYear, p_maxMonth, p_maxDay;
p_minDay = 1;
p_minMonth = 0;
p_minYear = 1950;
p_maxDay = 31;
p_maxMonth = 11;
p_maxYear = 2015;
Calendar m_maxDateCalendar = Calendar.getInstance();
m_maxDateCalendar.set(Calendar.YEAR, p_maxYear);
m_maxDateCalendar.set(Calendar.MONTH, p_maxMonth);
m_maxDateCalendar.set(Calendar.DAY_OF_MONTH, p_maxDay);
Calendar m_minDateCalendar = Calendar.getInstance();
m_minDateCalendar.set(Calendar.YEAR, p_minYear);
m_minDateCalendar.set(Calendar.MONTH, p_minMonth);
m_minDateCalendar.set(Calendar.DAY_OF_MONTH, p_minDay);
new MyPicker(m_context, m_datePickerListener, m_minDateCalendar,
m_maxDateCalendar, m_calendar).show();
} catch (Throwable p_e) {
// Have suppressed the exception
p_e.printStackTrace();
}
}
m_datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker p_view, int p_year,
int p_monthOfYear, int p_dayOfMonth) {
m_calendar.set(Calendar.YEAR, p_year);
m_calendar.set(Calendar.MONTH, p_monthOfYear);
m_calendar.set(Calendar.DAY_OF_MONTH, p_dayOfMonth);
date_picker.setText(""
+ m_dateFormat.format(m_calendar.getTime()));
}
};
答案 5 :(得分:0)
试试这个,
package com.mani.smsdetect;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Vibrator;
import android.support.annotation.Nullable;
public class SampleService extends Service{
Vibrator vibrator;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
vibrator.vibrate(2000);
return super.onStartCommand(intent, flags, startId);
}
}
答案 6 :(得分:0)
设置DatePickerDialog的最小值和最大日期是不够的。用户可以单击年份字段并选择任意年份。
您还应该在DatePicker上设置年份范围,如下所示:
datePicker.setYearRange(calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR) + 1);