我正在尝试制作类似提醒应用的内容。我想允许用户选择现在不是的日期和时间(至少从现在起5分钟),我还想禁止用户选择距离太远的日期 - 例如30天。我创建了datePicker和timePicker,使它们在按钮点击时弹出,但找不到设置最小值和最大值的方法。
private void galleryAddPic()
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file;
try
{
file = new File(mCurrentImagePath);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}catch(Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:7)
try this:
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
};
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog datePickerDialog = new DatePickerDialog(
Main23Activity.this, datePickerListener,
mYear, mMonth, mDay);
DatePicker datePicker = datePickerDialog.getDatePicker();
c.add(Calendar.MONTH, +1);
long oneMonthAhead = c.getTimeInMillis();
datePicker.setMaxDate(oneMonthAhead);
datePicker.setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
btnTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
}
};
Calendar c = Calendar.getInstance();
final TimePickerDialog timePickerDialog = new TimePickerDialog(Main23Activity.this,timePickerListener,
c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE)+5,false);
timePickerDialog.show();
}
});
您需要检查用户是否选择了今天的日期,但时间是否小于当前时间条件。
答案 1 :(得分:4)
对于DataOicker试试这个
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.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}
对于TimePicker,请尝试此exampel
答案 2 :(得分:3)
我合并了日期和时间选择器,并为timePicker添加了一个条件。
public class TestActivity extends Activity {
private Button mPickDate;
private TextView mDateDisplay;
private TextView mTimeDisplay;
final Calendar c = Calendar.getInstance();
private int mYear = c.get(Calendar.YEAR);
private int mMonth = c.get(Calendar.MONTH);
private int mDay = c.get(Calendar.DAY_OF_MONTH);
private int mHour = c.get(Calendar.HOUR_OF_DAY);
private int mMinute = c.get(Calendar.MINUTE);
static final int TIME_DIALOG_ID = 1;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
});
mDateDisplay = (TextView) findViewById(R.id.dateTextView);
mPickDate = (Button) findViewById(R.id.datepicker);
mTimeDisplay = (TextView) findViewById(R.id.timeTextView);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
});
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
// Update date and time
private void updateDate() {
mDateDisplay.setText(
new StringBuilder()
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "));
showDialog(TIME_DIALOG_ID);
}
public void updateTime() {
mTimeDisplay.setText(
new StringBuilder()
.append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
// Generate DatePickerDialog and TimePickerDialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
Calendar c2 = Calendar.getInstance();
if (mYear == c2.get(Calendar.YEAR)
&& mMonth == c2.get(Calendar.MONTH)
&& mDay == c2.get(Calendar.DAY_OF_MONTH)
&& (mHour < c2.get(Calendar.HOUR_OF_DAY) || (mHour == c2.get(Calendar.HOUR_OF_DAY) && mMinute <= (c2.get(Calendar.MINUTE)+10))
)
) {
Toast.makeText(TestActivity.this, "Set time at least 10 minutes from now", Toast.LENGTH_LONG).show();
} else {
updateTime();
}
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
c.add(Calendar.MONTH, +1);
long oneMonthAhead = c.getTimeInMillis();
datePickerDialog.getDatePicker().setMaxDate(oneMonthAhead);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return datePickerDialog;
case TIME_DIALOG_ID:
TimePickerDialog timePickerDialog =
new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
return timePickerDialog;
}
return null;}
}