特定时间禁用按钮
你好,实际上我的应用程序是按月计算的应用程序..所以我有按钮(paybtn),我应该点击它,它应该被禁用直到月底。然后应该会在下个月 1 日启用。我已经尝试过一些东西,当我单击时它会被禁用,但是当我的应用程序重新启动时它会再次启用,请有人帮助我,
直到现在我已经尝试过这个
Calendar today = Calendar.getInstance();
int dayofmonth = today.get(Calendar.DAY_OF_MONTH);
if (dayofmonth>1) {
holder.paybtn.setText("paid");
holder.paybtn.setEnabled(false);
}else if (dayofmonth == 1){
holder.paybtn.setEnabled(true);
}
有人请帮助我
Hello actually my app is monthly basis app.. so i have button (paybtn) on which i should click and it should get disabled till end of the month. and then should get enabled on 1st of next month. i have tried something it is getting disabled when i'm clicking but when my app is reastarted it is again getting enabled please someone help me
till now i have tried this
Calendar today = Calendar.getInstance();
int dayofmonth = today.get(Calendar.DAY_OF_MONTH);
if (dayofmonth>1) {
holder.paybtn.setText("paid");
holder.paybtn.setEnabled(false);
}else if (dayofmonth == 1){
holder.paybtn.setEnabled(true);
}
someone please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要解决三个问题:
记住操作
当用户执行每月的操作时,您需要记录这一事实。仅靠记忆进行跟踪是不够的。您需要在存储中记录这一事实,以防用户退出您的应用程序或您的应用程序崩溃。
Android 文档中介绍了此主题。 Stack Overflow 有许多现有的 有关此主题的问题和解答。
检查新月份
在应用程序启动时,检索上次执行操作时存储的时刻。
调整到您想要感知月份的月份。对于任何给定时刻,日期以及月份可能会因全球时区的不同而有所不同。
获取该时刻的年月。使用
YearMonth
类。与当前年份月份进行比较。
您还可以检查记录的年月是否在当前年月之后,因为这可能是问题/错误的症状。
在后台线程上检查当前月份
当您的应用程序运行时,您需要运行异步 在后台执行相同或相似检查的任务。
如果应启用按钮,请安排一些代码在 GUI 线程上运行以启用该按钮。 永远不要从 GUI 线程以外的任何线程访问或更新 GUI 小部件。
如何安排代码在 Android 上的 GUI 线程上运行超出了我的技能范围,正如我所做的那样不做Android工作。所有 GUI 框架(例如 Swing、JavaFX 和 Vaadin)都有自己的技术,用于向 GUI 线程发送请求以执行某些工作,Android 也不例外。这个主题似乎是 在 Stack Overflow 上被多次提及。
写入存储
当您的用户执行每月的家务时,单击该按钮,将这一刻记录在存储中。将时刻写入文本时,请使用标准 ISO 8601 格式。
Instant#toString
方法生成该格式的文本。避免遗留日期时间类
切勿使用
Date
/Calendar
类。这些可怕的遗留类在几年前就被 Java 8 及更高版本中定义的现代 java.time 类所取代。对于 Java 6 和7、使用ThreeTen-Backport库来获得大部分功能。
Android 26+ 捆绑了 java.time 的实现。对于早期的 Android,最新的工具通过“API 脱糖”提供了大部分 java.time 功能。
You have three problems to solve:
Remember action
When the user performs their monthly action, you need to record this fact. Keeping track in memory is not enough. You need to record this fact in storage in case the user quits your app or your app crashes.
This topic is covered on Android documentation. And Stack Overflow has many existing Questions and Answers on this topic.
Check for new month
On startup of your app, retrieve the stored moment when action was last taken.
Adjust into the month by which you want to perceive the month. For any given moment, the date, and therefore the month, may vary around the globe by time zone.
Get the year-month of that moment. Use the
YearMonth
class.Compare to current year-month.
You might also check for recorded year-month being after current year-month, as that would be symptomatic of a problem/bug.
Check current month on background thread
While your app is running, you need to run an asynchronous task in the background performing that same or similar check.
If button should be enabled, schedule some code to run on the GUI thread to enable that button. Never access or update the GUI widgets from any thread other than the GUI thread.
How you schedule code to run on the GUI thread on Android is beyond my skillset, as I do not do Android work. All GUI frameworks such as Swing, JavaFX, and Vaadin have their own technology for posting a request to the GUI thread to perform some work, and Android is no different. This topic seems to have been addressed many times on Stack Overflow.
Write to storage
When your user performs their monthly chore, clicking that button, record the moment in storage. When writing the moment as text, use standard ISO 8601 format. The
Instant#toString
method generates text in that format.Avoid legacy date-time classes
Never use
Date
/Calendar
classes. These terrible legacy classes were supplanted years ago by the modern java.time classes defined in Java 8 and later.For Java 6 & 7, use the ThreeTen-Backport library to get most of the functionality.
Android 26+ comes bundled with an implementation of java.time. For earlier Android, the latest tooling makes available most of the java.time functionality via "API desugaring".
这段代码只存在于按钮 onclick 中吗?您还必须在默认视图膨胀的情况下使用它。
Is this code only there in button onclick? You have to also use it where the default view is inflated.
程序结束后,所有值都将转储,您需要将值保存到文件中,以便可以在程序的新实例中打开它。我会研究 DataInputStream 和 DataOutputStream。
例如,您可以记录您刚刚完成的月份。
https://docs.oracle.com/javase/ 7/docs/api/java/io/DataInputStream.html
编辑:添加代码来回答
Once a program ends all values are dumped, you need to save your value to a file so it can be opened in your new instance of your program. I would look into DataInputStream and DataOutputStream.
You could for example log the month you just did.
https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
EDIT: added code to answer