[英]How to enable a button only on a specific time period in Android?
I'm working on Android using Java and I have a question.我正在使用 Java 研究 Android,我有一个问题。
I want a button to be enabled only during a certain time period.我希望仅在特定时间段内启用按钮。 Let's say I want the button to only work between 19:00 and 19:30 and after that it shows a error message, how would I achieve that?假设我希望该按钮仅在 19:00 和 19:30 之间工作,然后它显示一条错误消息,我将如何实现?
I tried using something like this code, but it didn't work out:我尝试使用类似这样的代码,但没有成功:
public class LocalTimeDemo {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
LocalTime time1 = LocalTime.parse("12:35:30");
System.out.println(time1.isBefore(time));
}
}
You have to use the calendar class for this.为此,您必须使用日历 class。 If you want to do it when activity is resumed then use this code snippet in the onResume method of activity如果您想在活动恢复时执行此操作,请在活动的 onResume 方法中使用此代码段
Calendar calendar = Calendar.getInstance();
int tHour = calendar.get(Calendar.HOUR_OF_DAY);
int tmin = calendar.get(Calendar.MINUTE);
if (tHour = 19 && tmin > 0 && tmin<30){
btn.setEnabled(true);
}else{
btn.setEnabled(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.