如何更新每5秒TextView的变量 [英] how to update the textview variable for every 5 seconds

查看:101
本文介绍了如何更新每5秒TextView的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
   TextView的更新每隔N秒?

在这里,我想在TextView的更新人力资源价值一旦被计算每次迭代,但随着每次2秒的延迟。我不知道该怎么做。我现在得到的TextView的是迭代的最后一个值。我想在一个恒定的延迟要被显示的所有值。任何帮助请。

 的(INT Y = 1; Y< p.length; Y ++)
    {
       如果(P [Y]!= 0)
        {
        R = P [Y] -p [Y-1];
          双X = R / 500;
          HR =(INT)(60 / X);
          视频下载(2000);
         的setText(将String.valueOf(HR));
      }
    }
 

解决方案

 公共类MainActivity延伸活动{
受保护的静态最后长TIME_DELAY = 5000;
//你的文本默认更新间隔,这是在你的手里,只要运行这​​个样本
TextView的mTextView;
处理程序处理程序=新的处理程序();
诠释计数= 0;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    mTextView =(TextView中)findViewById(R.id.textview);
    handler.post(updateTextRunnable);
}


可运行updateTextRunnable =新的Runnable(){
  公共无效的run(){
      算上++;
      mTextView.setText(获得所谓的+计数);
      handler.postDelayed(这一点,TIME_DELAY);
     }
 };
}
 

我希望这一次你将进入code和运行它。

Possible Duplicate:
Updating TextView every N seconds?

Here, I want to update the Hr value in the textview once it is calculated for every iteration, but with a delay of 2 seconds each time. I dont know how to do it. What i get now in the textview is the last value of the iteration. I want all the values to be displayed at a constant delay. anyone help pls.

    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));
      }
    }

解决方案

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);  
     }  
 };  
}

I hoped this time you will get into the code and run it .

这篇关于如何更新每5秒TextView的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆