2014-12-04 5 views
0

내 응용 프로그램에서 단추 및 TextView 조각이 있습니다. 매일 바뀌어야하는 TextView에 동적 메시지를 표시하려고합니다.매일 텍스트보기 메시지를 자동으로 변경하는 방법은 무엇입니까?

처음 버튼을 클릭하면 TextView에 "Day 1"이 표시되고 매일 (오전 12시) TextView는 자동으로 "day 2", "day 3", "day 10" 10 일간 지속되며, 버튼을 다시 클릭하면 TextView에 "THE END"메시지가 표시됩니다.

어떻게해야합니까?

답변

0

글쎄, Calendar 클래스를 사용하여 현재 날짜와 시간을 확인하십시오. 그런 다음 해당 정보를 사용하여 적절한 텍스트를 표시하십시오.

+0

답변 해 주셔서 감사합니다. 나는 안드로이드에 처음이다. 더 설명해 주시겠습니까? –

+0

'Calendar rightNow = Calendar.getInstance()'이것은 현재 날짜와 시간을 가진 객체를 제공합니다. 'Calendar' 객체로부터 정보를 얻으려면,'Calendar.get()', 즉'Calendar.get (Calendar.HOUR_OF_DAY)'를 사용하십시오. 기타 상수는 [Calendar] (http://developer.android.com/reference/java/util/Calendar.html#HOUR_OF_DAY)를 참조하십시오. 현재 날짜와 시간이 있다면 우리는 이미 새로운 날이 있는지 말할 수 있습니다. 예이면 버튼에 표시 할 일 수를 늘립니다. 이 번호를'SharedPreferences'에 저장하면 앱의 다음 시작 시점 사이에 잃어 버리지 않을 것입니다. –

+0

그 날짜와 현재 시간을 비교할 수 있도록'SharedPreferences'에 마지막으로 변경된 날짜를 저장해야합니다. –

0

기본적으로, 당신의 ONSTART 방법 :

 
String whatDayisIt = "Day 1"; 

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

난 당신이 버튼 로직에 대한 자세한 내용을 묻고 실제로 무슨 요일 파악하고 있으리라 믿고있어.

+0

나는 이것을 알고 있지만 시스템 날짜가 바뀌면 2 일째에 어떻게 변하는가? –

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를 사용하여 요일을 저장, exemple를 위해해야 ​​

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 
} 

완전한 코드는 아니지만 로직을 볼 수 있습니다.

희망이 있습니다.

관련 문제