简体   繁体   English

在Android中定期更新TextView

[英]Updating a textview periodically in android

I'm currently developing an android application. 我目前正在开发一个android应用程序。 It needs to update a textview value periodically. 它需要定期更新textview值。

For instance, I want to increase the value by 10 each second. 例如,我想每秒增加10个值。 I tried with the following code, but it is not working fine : the textview is only updated after the increment is finished 我尝试使用以下代码,但无法正常工作:仅在增量完成后才更新textview

package com.example.stack1;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity{
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
    public void onResume(){
        super.onResume();
        TextView output=(TextView) findViewById(R.id.output);
        output.setText(String.valueOf(0));
        System.out.println(0);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        output.setText(String.valueOf(10));
        System.out.println(10);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        output.setText(String.valueOf(20));
        System.out.println(20);
    }
}

output is a textview in main.xml file. output是main.xml文件中的textview。 This file only contains this object. 该文件仅包含该对象。

Note- Expected output in textview is "0", after 10 second "10" after 20 seconds "20". 注意:textview的预期输出为“ 0”,在20秒“ 20”之后的10秒为“ 10”。 However, with this code, the output is blank until 20 seconds , then "20" appears. 但是,使用此代码,输出将一直空白直到20秒,然后显示“ 20”。

You'll probably want to use something like AlarmManager instead of sleep() as sleep() will stop everything going on in the process during the sleep time. 您可能想要使用AlarmManager之类的东西而不是sleep()因为sleep()会在睡眠时间内停止该过程中的所有操作。

The reason it's not showing anything is that you're doing it in the onResume() method. 它没有显示任何内容的原因是您正在onResume()方法中进行操作。 So when it loads up the display, before it actually shows anything, it's processing that method to get things set up for the user. 因此,当它加载显示内容时,在它实际显示任何内容之前,它正在处理该方法以为用户进行设置。

So, what you want is: 因此,您想要的是:

  1. Load display with value 0 加载显示值为0
  2. Wait 10 seconds 等待10秒
  3. Increment and display to user 递增并显示给用户
  4. Wait 10 seconds 等待10秒
  5. Increment and display to user 递增并显示给用户

What you're getting because of the sleep() commands is: 您通过sleep()命令得到的是:

  1. Set value to 0 将值设为0
  2. Wait 10 seconds 等待10秒
  3. Set value to 10 将值设置为10
  4. Wait 10 seconds 等待10秒
  5. Set value to 20 将值设置为20
  6. Display value 显示值

With an AlarmManager, you initialise that in your onResume() method, the display loads, then the AlarmManager increments the value every ten seconds. 使用AlarmManager,您可以在onResume()方法中初始化显示加载,然后AlarmManager每十秒钟递增一次值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM