2014-12-04 24 views
0

在我的應用程序中,我有一個帶有按鈕和TextView的片段。 我想在TextView中顯示一個應該每天都會改變的動態消息。當我第一次點擊按鈕時,TextView應該顯示「第1天」,並且在每天(12點)之後,TextView會自動更改爲「第2天」,「第3天」,直到「第10天」持續10天,如果再次點擊按鈕,TextView將顯示消息「THE END」。如何自動更改文本查看消息?

我應該怎麼做?

回答

0

那麼,使用Calendar類來檢查當前的日期和時間。然後使用該信息來顯示適當的文本。

+0

感謝您的回答。我是Android新手。你能解釋一下嗎? – 2014-12-04 21:30:11

+0

'日曆rightNow = Calendar.getInstance()'這會爲您提供當前日期和時間的對象。要從'Calendar'對象獲取信息,請使用'Calendar.get()',即'Calendar.get(Calendar.HOUR_OF_DAY)'。查看更多常量[日曆](http://developer.android.com/reference/java/util/Calendar.html#HOUR_OF_DAY)。如果你有當前的日期和時間,那麼你就可以說我們是否已經有了新的一天。如果是,則增加您在按鈕上顯示的日數。您可以將此編號存儲在'SharedPreferences'中,以便在您的應用下次啓動時不會丟失。 – 2014-12-04 21:36:07

+0

您還應該在'SharedPreferences'中存儲上次更改日期的日期,以便能夠比較該日期和當前時間。 – 2014-12-04 21:37:59

0

基本上,你的OnStart方法:

 
String whatDayisIt = "Day 1"; 

Button b = (Button) findViewByID(R.id.YOUR_BUTTON_NAME) 
b.setText(whatDayisIt) 

我假設你問更多的按鈕邏輯和搞清楚什麼日子它實際上是。

+0

我知道這一點,但是當系統日期發生變化時,它如何更改爲第2天? – 2014-12-04 21:29:05

0

這是我對其採取 注意:您可能需要存儲當前count值,當應用程序被殺害的count之前的值會有所回升,以實現最簡單的方法是使用SharedPreferences

Calendar calendar = Calendar.getInstance(); // retrieve an instance of the calendar to query Time-related info 

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); //retrieve an instance of the SharedPreferences to store primitive data value. 
SharedPreferences.Editor edtior = pref.edit();//every change to the preferences must pass through an SharedPreferences.Editor, so call pref.edit() to retrive one; 

int count = 1; //to keep track of the current day 
while (count <= 10) 
{ 

    if (calendar.get(Calendar.HOUR) == 12 && calendar.get(Calender.AM_PM) == Calendar.AM) // if it is at 12 AM in the morning 
    { 
     count = pref.getInt("count", 1);//retrieve the count value if it is saved previously with an identifier of count, otherwise, return 1 as a default value. 
     textView.setText("Day " + count); 
     count++;//indicate that new day has come 

     if(this.isFinishing())//check if the user is leaving the app by pressing the back key, if true, we need to store the current count value which is the current day 
     { 
      editor.putInt("count", count); 
      editor.commit(); //need to commit the change to persist it 
     } 

    } 
} 

text.setText("The End"); 
0

那麼,我假設你知道如何設置和顯示一個TextView。

1)您需要存儲第一次點擊按鈕時的日期(關閉您的應用程序並保證您的數據安全)。 您應該使用SharedPreferences是什麼,以及存儲一週的某一天,對於爲例:

Calendar c = Calendar.getInstance(); 
int day = c.get(Calendar.DAY_OF_WEEK); // Or other Calendar value 

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
SharedPreferences.Editor editor = shared.edit(); 
editor.putInt(DAY_START, day); 
editor.commit(); // commit is important here. 

2)你的活動的onCreate方法,得到優先一天的開始。

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
int dayStart = (shared.getInt(DAY_START, 1)); 

3)要知道,我們是哪天,只值。減去

int today = day - dayStart; // Add +1 if u want to say "day 1" for 0 value 

4)再次使用日曆類來獲得一天的小時。

5)只需添加一個條件來顯示END消息。

If (today == 10) { 
    // message 
} 

這不是一個完整的代碼,但你可以看到邏輯。

希望它有幫助。