使用 OnResume() 重新加载当前时间
这是我在 src 中的 .java 文件:
package com.wao.texttime;
import java.util.Calendar;
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 textView = (TextView) findViewById(R.id.TextView01);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 6);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 0);
long vask_1 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_2 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 14);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_3 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_4 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_5 = cal.getTimeInMillis();
long now = System.currentTimeMillis();
if(now > vask_1 && now < vask_2)
{
textView.setText("5");
}
else if(now > vask_2 && now < vask_3)
{
textView.setText("1");
}
else if(now > vask_3 && now < vask_4)
{
textView.setText("2");
}
else if(now > vask_4 && now < vask_5)
{
textView.setText("3");
}
}
}
我被告知可以使用 OnResume 重新加载当前时间以更新 TextView 中的文本。但是,我不知道如何在文件中使用它。您有什么建议吗?
This is my .java file in src:
package com.wao.texttime;
import java.util.Calendar;
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 textView = (TextView) findViewById(R.id.TextView01);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 6);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 0);
long vask_1 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_2 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 14);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_3 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_4 = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long vask_5 = cal.getTimeInMillis();
long now = System.currentTimeMillis();
if(now > vask_1 && now < vask_2)
{
textView.setText("5");
}
else if(now > vask_2 && now < vask_3)
{
textView.setText("1");
}
else if(now > vask_3 && now < vask_4)
{
textView.setText("2");
}
else if(now > vask_4 && now < vask_5)
{
textView.setText("3");
}
}
}
I've been told I can use the OnResume to reload the current time to update the text in the TextView. But, I don't know how to use it in the file. Do you have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
onCreate
方法结束}
后,添加以下内容:您还必须将
TextView
的声明移到onCreate 之外
方法,或者至少您可能想要这样做。为此,请在onCreate
方法之前添加如下行:然后在
onCreate
中,您只需要这样:然后您就可以访问
textView
也可以从您的onResume
方法中获取。希望有帮助。
After the closing
}
of youronCreate
method, add this:You will also have to move the declarations of your
TextView
outside theonCreate
method, or at least you will probably want to. To do this, add a line like this before theonCreate
method:and then in your
onCreate
you just need this:Then you can access
textView
from youronResume
method too.Hope that helps.