如何在 android 中为日期选择器设置最小和最大限制?
How to set a minimum and maximum limit for a date picker in android?
我正在开发一个使用自定义日期和时间选择器的应用程序。
我创建了一个对话框,里面分别显示日期和时间选择器,现在我想为日期选择器设置最小和最大限制。
比如一个人不应该能够 select 从今天开始的前一个日期并且不超过一个月。
这是我的代码
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final DatePicker picker = new DatePicker(getActivity());
try {
Field f[] = picker.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(picker);
((View) yearPicker).setVisibility(View.GONE);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
picker.setCalendarViewShown(false);
picker.setMinDate(new Date().getTime());
builder.setTitle("Please select date on which you would be leaving :")
.setView(picker)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE dd MMM");
Date date1 = new Date();
date1.setDate(picker.getDayOfMonth());
date1.setMonth(picker.getMonth());
final String s = parseFormat.format(date1);
Log.e("DATE", s);
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
Date date2=new Date();
date2.setDate(picker.getDayOfMonth());
date2.setMonth(picker.getMonth());
final String s2=sdf.format(date2);
//Time picker
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final TimePicker picker = new TimePicker(getActivity());
picker.setIs24HourView(true);
builder.setTitle("Please select time at which you would be leaving :")
.setView(picker)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int hour = picker.getCurrentHour();
int minute = picker.getCurrentMinute();
leaving.setTextSize(14);
leaving.setText(s + " " + hour + ":" + minute);
inTime=s2+" "+hour+":"+minute;
Log.e("inTime ",inTime);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
leaving.setText("");
inTime="";
dialog.dismiss();
}
}).create().show();
//Time picker
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
leaving.setText("");
inTime="";
dialog.dismiss();
}
}
)
.create().show();
请帮忙。谢谢
使用此代码。
我在我的应用程序中使用此代码。
// Global declaration
Calendar preferDate1 = new GregorianCalendar();
Calendar preferDate2 = new GregorianCalendar();
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
private int year, month, day;
// here preferredDate1 and preferredDate2 are textview
preferredDate1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp = new DatePickerDialog(MainActivity.this, prefferedDate1Picker, preferDate1
.get(Calendar.YEAR), preferDate1.get(Calendar.MONTH), preferDate1.get(Calendar.DAY_OF_MONTH));
dp.getDatePicker().setMinDate(c.getTimeInMillis());
dp.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate1Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate1 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay);
preferredDate2.setText("");
preferredDate1.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
// This will set date based on preferredDate1
// change here according to your need. If you want to add or remove one month then set based on data. In this I add one day in second data picker.
preferredDate2.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp2 = new DatePickerDialog(MainActivity.this, prefferedDate2Picker, preferDate2
.get(Calendar.YEAR), preferDate2.get(Calendar.MONTH), preferDate2.get(Calendar.DAY_OF_MONTH));
long mili = preferDate1.getTimeInMillis() + 86400000L;
dp2.getDatePicker().setMinDate(mili);
dp2.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate2Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate2 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay+1);
preferredDate2.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
试试这个,这会在日期选择器对话框中显示从今天到下一个 5 年的日期。
public class MainActivity extends Activity {
TextView txtDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cal = Calendar.getInstance();
cal1 = Calendar.getInstance();
txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
txtDatePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}
我正在开发一个使用自定义日期和时间选择器的应用程序。 我创建了一个对话框,里面分别显示日期和时间选择器,现在我想为日期选择器设置最小和最大限制。 比如一个人不应该能够 select 从今天开始的前一个日期并且不超过一个月。 这是我的代码
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final DatePicker picker = new DatePicker(getActivity());
try {
Field f[] = picker.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(picker);
((View) yearPicker).setVisibility(View.GONE);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
picker.setCalendarViewShown(false);
picker.setMinDate(new Date().getTime());
builder.setTitle("Please select date on which you would be leaving :")
.setView(picker)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE dd MMM");
Date date1 = new Date();
date1.setDate(picker.getDayOfMonth());
date1.setMonth(picker.getMonth());
final String s = parseFormat.format(date1);
Log.e("DATE", s);
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
Date date2=new Date();
date2.setDate(picker.getDayOfMonth());
date2.setMonth(picker.getMonth());
final String s2=sdf.format(date2);
//Time picker
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final TimePicker picker = new TimePicker(getActivity());
picker.setIs24HourView(true);
builder.setTitle("Please select time at which you would be leaving :")
.setView(picker)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int hour = picker.getCurrentHour();
int minute = picker.getCurrentMinute();
leaving.setTextSize(14);
leaving.setText(s + " " + hour + ":" + minute);
inTime=s2+" "+hour+":"+minute;
Log.e("inTime ",inTime);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
leaving.setText("");
inTime="";
dialog.dismiss();
}
}).create().show();
//Time picker
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
leaving.setText("");
inTime="";
dialog.dismiss();
}
}
)
.create().show();
请帮忙。谢谢
使用此代码。
我在我的应用程序中使用此代码。
// Global declaration
Calendar preferDate1 = new GregorianCalendar();
Calendar preferDate2 = new GregorianCalendar();
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
private int year, month, day;
// here preferredDate1 and preferredDate2 are textview
preferredDate1.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp = new DatePickerDialog(MainActivity.this, prefferedDate1Picker, preferDate1
.get(Calendar.YEAR), preferDate1.get(Calendar.MONTH), preferDate1.get(Calendar.DAY_OF_MONTH));
dp.getDatePicker().setMinDate(c.getTimeInMillis());
dp.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate1Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate1 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay);
preferredDate2.setText("");
preferredDate1.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
// This will set date based on preferredDate1
// change here according to your need. If you want to add or remove one month then set based on data. In this I add one day in second data picker.
preferredDate2.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
DatePickerDialog dp2 = new DatePickerDialog(MainActivity.this, prefferedDate2Picker, preferDate2
.get(Calendar.YEAR), preferDate2.get(Calendar.MONTH), preferDate2.get(Calendar.DAY_OF_MONTH));
long mili = preferDate1.getTimeInMillis() + 86400000L;
dp2.getDatePicker().setMinDate(mili);
dp2.show();
}
});
private final DatePickerDialog.OnDateSetListener prefferedDate2Picker = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
preferDate2 = new GregorianCalendar(selectedYear, selectedMonth, selectedDay+1);
preferredDate2.setText((selectedDay < 10 ? "0" + String.valueOf(selectedDay) : String.valueOf(selectedDay)) + "/"
+ (selectedMonth + 1 < 10 ? "0" + String.valueOf((selectedMonth + 1)) : String.valueOf((selectedMonth + 1)))
+ "/" + selectedYear);
}
};
试试这个,这会在日期选择器对话框中显示从今天到下一个 5 年的日期。
public class MainActivity extends Activity {
TextView txtDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cal = Calendar.getInstance();
cal1 = Calendar.getInstance();
txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
txtDatePicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}