你好實際上我的應用程式是按月計算的應用程式..所以我有按鈕(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);
}
有人請幫幫我
uj5u.com熱心網友回復:
你需要解決三個問題:
- 記住何時采取了每月行動。
- 在應用程式啟動時和在應用程式運行時在后臺檢查滾動到新月份。
- 從傳統日期時間類切換到現代java.time類。
記住動作
當用戶執行他們每月的操作時,您需要記錄這個事實。在記憶體中跟蹤是不夠的。您需要在存盤中記錄這一事實,以防用戶退出您的應用程式或您的應用程式崩潰。
此主題在Android 檔案中有所介紹。Stack Overflow 有很多關于這個主題的現有問題和答案。
檢查新的一個月
在您的應用程式啟動時,檢索上次執行操作的存盤時刻。
Instant instant = Instant.parse( "2022-03-09T03:51:35.013744Z" ) ;
調整到您想要感知月份的月份。對于任何給定的時刻,日期以及月份可能會因時區而在全球范圍內有所不同。
ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
獲取那一刻的年月。使用YearMonth類。
YearMonth yearMonthRecorded = YearMonth.from( zdt ) ;
與當前年月比較。
YearMonth yearMonthCurrent = YearMonth.now( z ) ;
if( yearMonthRecorded.isBefore( yearMonthCurrent ) ) {
… enable your button
} else {
… disable your button
}
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.
Instant instant = Instant.now() ;
String momentIso8601Format = instant.toString() ;
Avoid legacy date-time classes
永遠不要使用Date/Calendar類。這些可怕的遺留類在幾年前被Java 8 及更高版本中定義的現代java.time類所取代。
對于 Java 6 和 7,使用ThreeTen-Backport庫來獲得大部分功能。
Android 26 捆綁了java.time的實作。對于早期的 Android,最新的工具通過“API 脫糖”提供了大部分java.time功能。
uj5u.com熱心網友回復:
此代碼僅在按鈕 onclick 中存在嗎?您還必須在默認視圖膨脹的地方使用它。
uj5u.com熱心網友回復:
一旦程式結束,所有值都被轉儲,您需要將值保存到檔案中,以便可以在程式的新實體中打開它。我會研究 DataInputStream 和 DataOutputStream。
try {
File tempFile = new File("date.dat");
file = new FileOutputStream(tempFile);
file.write(today.MONTH);
file.close();
}catch(IOException e) {
System.err.println("Exception\n" e.toString());
}
例如,您可以記錄您剛剛完成的月份。
https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
編輯:添加代碼來回答
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442410.html
上一篇:Pythondatetime到Excel序列日期轉換
下一篇:通過檢查上一行中的值獲取最新行
