使用的setText提供有关当前时间规则 [英] Using setText with rules regarding the current time

查看:190
本文介绍了使用的setText提供有关当前时间规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在我的src 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");
    }
}

我想TextView的显示取决于它是什么时候了不同的文本。 F.ex. 08间:45-10:45,等等。我是很新的既针对Android和Java编码,我试图找出我怎么能做出哪些文本显示的规则。你有什么建议吗?

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?

推荐答案

这可能是一个比较复杂一点。我可能会用下面的办法(withh有点优化OFC的。)

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

这篇关于使用的setText提供有关当前时间规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆