使用 OnResume() 重新加载当前时间

发布于 2024-10-04 11:35:55 字数 2027 浏览 0 评论 0原文

这是我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-10-11 11:35:55

onCreate 方法结束 } 后,添加以下内容:

@Override
protected void onResume(){
    super.onResume();
    // code to update the date here
}

您还必须将 TextView 的声明移到 onCreate 之外 方法,或者至少您可能想要这样做。为此,请在 onCreate 方法之前添加如下行:

private TextView textView;

然后在 onCreate 中,您只需要这样:

textView = (TextView) findViewById(R.id.TextView01);

然后您就可以访问 textView也可以从您的 onResume 方法中获取。

希望有帮助。

After the closing } of your onCreate method, add this:

@Override
protected void onResume(){
    super.onResume();
    // code to update the date here
}

You will also have to move the declarations of your TextView outside the onCreate method, or at least you will probably want to. To do this, add a line like this before the onCreate method:

private TextView textView;

and then in your onCreate you just need this:

textView = (TextView) findViewById(R.id.TextView01);

Then you can access textView from your onResume method too.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文