我有一个带有今天日期的文本视图和两个按钮,当onclick将文本视图设置为下一个或上一个日期。现在我想要在textview显示当前日期时禁用上一个按钮。也就是说,如果今天的日期是1/3,用户可以转到1/4及之后在未来日期之间切换。但是用户不能在当前日期之前进入1/2或任何日期。
json
答案 0 :(得分:1)
尝试此功能:
public boolean Check_valid_date(CharSequence d) {
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
Date date_new = null;
try {
date_new = sdf.parse(d.toString());
Date current_date = new Date();
current_date = removeTime(current_date);
Log.d("CurentDate", current_date.toString());
Log.d("NEW date", date_new.toString());
if (current_date.equals(date_new)) {
return true;
}
return current_date.before(date_new);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
public Date removeTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
将其用作:
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
CharSequence date = DateFormat.format("dd MMMM yyyy", calendar.getTime());
//pass your selected date..
boolean check = Check_valid_date(date);
if(check){
//date is after current date
}
else{
//date is before current date
//disable button
}
答案 1 :(得分:0)
在编码中执行此操作
myButton.setEnabled(false)