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

时间:2010-11-29 00:00:33

标签: android 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中的文本。但是,我不知道如何在文件中使用它。你有什么建议吗?

1 个答案:

答案 0 :(得分:2)

}方法的结束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

希望有所帮助。