每5秒更新TextView变量的方法

3

可能重复:
如何每N秒更新TextView?

这里,我想在每次计算后,将Hr值更新到textview中,但每次都要有2秒的延迟。我不知道该怎么做。现在,textview中显示的是迭代的最后一个值。我希望所有的值以恒定的时间间隔显示出来。请帮忙解决。

    for(int y=1;y<p.length;y++)
    {
       if(p[y]!=0)
        {
        r=p[y]-p[y-1];
          double x= r/500;
          Hr=(int) (60/x);
          Thread.sleep(2000);
         settext(string.valueof(Hr));
      }
    }

你可以使用倒计时器。 - skygeek
或者在 for 循环中使用 postDelayed - thepoosh
5个回答

4
public class MainActivity extends Activity{
protected static final long TIME_DELAY = 5000;
//the default update interval for your text, this is in your hand , just run this sample
TextView mTextView;
Handler handler=new Handler();  
int count =0;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView=(TextView)findViewById(R.id.textview);
    handler.post(updateTextRunnable);
}


Runnable updateTextRunnable=new Runnable(){  
  public void run() {  
      count++;
      mTextView.setText("getting called " +count);
      handler.postDelayed(this, TIME_DELAY);  
     }  
 };  
}

我希望这一次你能进入代码并运行它。

TIME_DELAY 的变化发生在哪里? - Pooja
你可以定义初始偏移量,然后它会在一定延迟后更新你的TextView。比如你设置TIME_DELAY = 5000,那么你的文本将每5秒更新一次。你只需要在mTextView.setText("你要更新的文本内容")中更改你的文本即可。 - Vipin Sahu
我不明白。我应该在哪里更改延迟值?请帮忙。 - Pooja
Runnable 将在每 5 秒间隔调用。 - Vipin Sahu

2

你应该使用计时器类...

Timer timer = new Timer();
        timer.schedule(new TimerTask() {

        public void run() {


        }, 900 * 1000, 900 * 1000);

上面的代码是用于每15分钟执行一次。更改此值并在您的情况下使用......

我们需要使用runOnUiThread()方法从TimerTask线程更新TextView。 - ρяσѕρєя K
谢谢回答。我是Android的新手。我尝试了这个,但是它没有起作用。我哪里做错了?请帮忙。for(int y=1;y<p.length;y++) { if(p[y]!=0) { r=p[y]-p[y-1]; double x= r/500; Hr=(int) (60/x); Timer timer = new Timer(); timer.schedule(new TimerTask() {public void run() { settext(string.valueof(Hr)); }, 9 * 1000, 9 * 1000); } } - Pooja
每5分钟更新应该写成300,而不是9...干杯 - user1755441
我的应用程序被强制关闭了。那么,我尝试的方式是否正确?我只想要5秒钟的延迟,我该怎么做? - Pooja
参数 task-->要调度的任务。 delay-->首次执行前的等待时间(以毫秒为单位)。 period-->连续执行之间的时间间隔(以毫秒为单位)。因此,您应该在延迟时间和周期中写入5 * 1000以表示5秒。 - user1755441

2

使用HandlerTimerTask(带有runOnUiThread())来更新文本,而不是使用for循环每5秒更新一次,例如:

Handler handler=new Handler();  

handler.post(runnable);  
Runnable runnable=new Runnable(){  
  @Override  
    public void run() {  
      settext(string.valueof(Hr));  //<<< update textveiw here
      handler.postDelayed(runnable, 5000);  
     }  
 };  

1

你能给我一个例子吗? - Pooja

0

希望这能对您有足够的帮助

import java.awt.Toolkit;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class demo 
{
  Toolkit toolkit;
  Timer timer;
  public demo()
  {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new scheduleDailyTask(), 0, //initial delay
        2 * 1000); //subsequent rate
  }
  class scheduleDailyTask extends TimerTask 
  {
    public void run() 
    {
      System.out.println("this thread runs for every two second");
      System.out.println("you can call this thread to start in your activity");
      System.out.println("I have used a main method to show demo");
      System.out.println("but you should set the text field values here to be updated simultaneouly");
    }
  }
  public static void main(String args[]) {
    new demo();
  }
}

我尝试了这个... 它作为Java应用程序完美运行。但是,在我的应用程序中,当我从主活动调用该类时,工具包=Toolkit.getDefaultToolkit()出现错误,并且我的图表活动也受到影响。有没有办法在后台异步地执行此操作? - Pooja
请参考http://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using-asynctask-on-android/,该示例使用后台任务,在状态栏中报告进度。虽然不完全符合您的需求,但可以帮助您入门。如有需要,请随时与我联系。 - Hussain Akhtar Wahid 'Ghouri'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接