【发布时间】:2014-09-18 02:07:12
【问题描述】:
好的,我正在创建 Android 应用程序,它与时间有关。例如,03.00 到 05.30 点 textView 将设置文本“下午好”,否则“晚上好”。请分享一些解决方案,我真的对那个算法感到困惑,谢谢
【问题讨论】:
-
我们不会为您编写代码。向我们展示您的尝试,我们将帮助您解决问题。
好的,我正在创建 Android 应用程序,它与时间有关。例如,03.00 到 05.30 点 textView 将设置文本“下午好”,否则“晚上好”。请分享一些解决方案,我真的对那个算法感到困惑,谢谢
【问题讨论】:
这是一个可以解决您的目的的代码。概念很简单。只需花时间从日历类中进行验证,并将其与当前时间进行比较然后采取适当的行动。
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");
}
【讨论】:
首先你得到设备的时间然后你检查条件
if(time>=03:00 && time<=5:30){
textview.setText("good afternoon");
}else{
textview.setText("good evevning");
}
【讨论】: