【问题标题】:android update textview for every 30 seconds [duplicate]android每30秒更新一次textview [重复]
【发布时间】:2016-11-07 00:44:51
【问题描述】:

我想从0th 秒开始每隔 30 秒启动一个计时器,

我需要将 textview 更新为 0.5min, 1.0min, 1.5min

我该怎么做?

【问题讨论】:

标签: java android android-studio time textview


【解决方案1】:

试试这个代码,

  final Handler h = new Handler();
            h.post(new Runnable() {
                @Override
                public void run() {

                    long millis = (long) currentTime();

                    dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS"));

                    h.postDelayed(this, 1000);
                }
            });

【讨论】:

  • 如何获取 textview 的增量值?
  • textview 中增加的值是什么意思?
  • 我的意思是在 run() 中的代码中,我应该为我的 tetxview 分配什么值,以便它显示增量值,例如 0.5min、1.0min 等
  • 检查更新的答案@Samuel909
【解决方案2】:

我们可以使用handler 来处理这个问题。

你可以定义如下

Handler mHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        // write your code update textview

        mHandler.sendEmptyMessageDelayed(0, 30000);
        return true;

    }
})

此处理程序将再次自动发送和更新文本。您还需要插入代码来更新 TextView 的时间。

别忘了在第一时间调用处理程序:mHandler.sendEmptyMessage(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    相关资源
    最近更新 更多