1

这是我在 src 中的 .java 文件:

package com.wao.texttime;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TextTime extends Activity {
    /** Called when the activity is first created. */
    @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 之间等等。我对 android 和 java 的编码都很陌生,我试图弄清楚如何为要显示的文本制定规则。你有什么建议吗?

4

3 回答 3

1

它可能会更复杂一些。我可能会使用以下方法(对c进行一些优化。)

    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");
    }
于 2010-11-28T22:14:28.953 回答
0

使用时间类获取当前时间。然后使用 switch 语句或 if/else if/else 块(取决于您的确切要求)来显示不同的文本。

于 2010-11-28T21:43:33.037 回答
0

您可以使用日历来获取当天的当前时间。

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
于 2010-11-28T21:43:44.900 回答