简体   繁体   English

如何仅在 Android 中的特定时间段启用按钮?

[英]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.

相关问题 如何在应用程序中仅在特定时间段内启用某些功能? - How to enable certain functions only for a specific period of time in apps? 在特定时间段内禁用Android应用上的按钮 - Disable button on an Android app for a specific period of time 如何在Android中确定在指定时间段内按下音量按钮 - How to Identify pressing the volume button for a specified time period in android 如何限制Android应用程序仅在特定时间段内运行? - How to restrict the Android application to run only a certain time period? 如何在日期选择器 Android 中仅启用特定日期? - How can I enable only specific days in datepicker Android? 在Java中,如何仅选择或过滤在特定时间段内创建的文件 - In Java, how to only pick or filter files created between during a specific time period 禁用按钮一段时间 - Disable a button for a period of time 如何在Android程序中添加时间段? - How to add time period in Android program? 如何一般地对时间范围建模,以允许在任何时间段或更具体的时间段内使用范围 - How to model a time range generically that will allow a range for any period of time, or a more specific period of time 计算特定时间段内的数字,Java,Android - Count up numbers in a specific time period, Java, Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM