将 setText 与有关当前时间的规则结合使用

发布于 2024-10-05 05:56:59 字数 741 浏览 0 评论 0原文

这是我在 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 根据时间显示不同的文本。 F. 前。 08:45-10:45 之间等等。我对 android 和 java 编码都很陌生,我正在尝试弄清楚如何制定显示文本的规则。您有什么建议吗?

This is my .java file in src:

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");
    }
}

I want the TextView to display the different texts depending on what time it is. F.ex. between 08:45-10:45 and so forth. I am very new to coding both for android and in java and I'm trying to figure out how I can make the rules for which text to display. Do you have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素年丶 2024-10-12 05:56:59

可能有点复杂。我可能会使用以下方法(进行一些优化)。

    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");
    }

It might be a bit more complicated. I would probably use the following approach (withh a bit of optimization ofc.)

    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");
    }
剧终人散尽 2024-10-12 05:56:59

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

Use the time class to get the current time. Then use switch statements or if/else if/else blocks (depending on your exact requirements) to display different texts.

爱殇璃 2024-10-12 05:56:59

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

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

You could use a Calender to get the current hour of the day.

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文