【问题标题】:How to set text with a predetermined time android如何用预定时间设置文本android
【发布时间】:2016-05-22 07:06:04
【问题描述】:

如何将文本替换为预定时间示例:12:00 文本变为下午,3:00 文本变为晚上好? 请解释一下:)

我的自定义文本:

public texttime(Context ct)
{
    super(ct);
}
public texttime(Context ct,AttributeSet attrs)
{
    super(ct,attrs);
    //start
    if(Time.is....(12:00)
    {
        setText("good afternoon");
    }
}

谢谢:)

【问题讨论】:

    标签: java android textview


    【解决方案1】:

    您可以从Calender 类的实例中获取当前时间,如下所示

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

    然后你可以使用下面的 if-else 阶梯

    if(nowHour>=0 &&<nowHour<=6)
    {
      textView.setText("late night");
    }
    else if(nowHour>=7 &&<nowHour<=11)
    {
       textView.setText("morning");
    }
    else if(nowHour>=12 &&<nowHour<=15)
    {
       textView.setText("noon");
    }
    else if(nowHour>=16 &&<nowHour<=18)
    {
       textView.setText("afternoon");
    } //and so on
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2019-03-23
      相关资源
      最近更新 更多