這是要實現的代碼:
package com.wao.texttime;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TextTime extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Good Morning");
TextView tv2 = (TextView) findViewById(R.id.TextView02);
tv1.setText("Good Afternoon");
}
}
我想在TextView根據時間來顯示不同的文本。比如在08:45-10:45之間顯示什麼文本等等。請問有什麼好的建議嗎?
稍微有點復雜,希望幫的上你:
TextView textView = (TextView) findViewById(R.id.text);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 45);
cal.set(Calendar.SECOND, 0);
long morning_start = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
long morning_end = cal.getTimeInMillis();
long now = System.currentTimeMillis();
if(now > morning_start && now < morning_end)
{
textView.setText("Good morning");
}
else
{
textView.setText("Have a nice day");
}