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

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

问题描述

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

在这里,我想在每次迭代计算后更新 textview 中的 Hr 值,但每次延迟 2 秒.我不知道该怎么做.我现在在 textview 中得到的是迭代的最后一个值.我希望以恒定延迟显示所有值.任何人请帮忙.

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天全站免登陆