【问题标题】:android: textView.setText when time 03.00 until 05.30 [closed]android:textView.setText 时间 03.00 到 05.30 [关闭]
【发布时间】:2014-09-18 02:07:12
【问题描述】:

好的,我正在创建 Android 应用程序,它与时间有关。例如,03.00 到 05.30 点 textView 将设置文本“下午好”,否则“晚上好”。请分享一些解决方案,我真的对那个算法感到困惑,谢谢

【问题讨论】:

  • 我们不会为您编写代码。向我们展示您的尝试,我们将帮助您解决问题。

标签: android datetime


【解决方案1】:

这是一个可以解决您的目的的代码。概念很简单。只需花时间从日历类中进行验证,并将其与当前时间进行比较然后采取适当的行动。

TextView textView = (TextView) findViewById(R.id.text);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());

cal.set(Calendar.HOUR_OF_DAY, 3);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 0);

long noon_start = cal.getTimeInMillis();//3.00

cal.set(Calendar.HOUR_OF_DAY, 5);
cal.set(Calendar.MINUTE, 30);

long noon_end = cal.getTimeInMillis();//5.30
long now = System.currentTimeMillis();//current time

if(now > noon_start && now < noon_end)
{
    textView.setText("Good afternoon");
}
else
{
    textView.setText("Good Evening");
}

【讨论】:

  • 太棒了。 .谢谢kgandroid,你让我的生活变得轻松。 .
【解决方案2】:

首先你得到设备的时间然后你检查条件

   if(time>=03:00 && time<=5:30){
    textview.setText("good afternoon");
     }else{
        textview.setText("good evevning");
      }

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多