本文转自:http://blog.csdn.net/macong01/article/details/7479266

本想做一个软件可以对UI界面进行定时更新,找了一些资料,先贴一个简单的定时更新界面程序,可以实现每隔1秒递增计数器的功能。

界面布局文件main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextViewandroid:id="@+id/counter"android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:text="Count: 0"/>
  7. <LinearLayoutandroid:orientation="horizontal"
  8. android:layout_width="fill_parent"android:layout_height="wrap_content">
  9. <Buttonandroid:text="start"android:id="@+id/Button01"
  10. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
  11. <Buttonandroid:text="stop"android:id="@+id/Button02"
  12. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:enabled="false"></Button>
  13. <Buttonandroid:text="reset"android:id="@+id/Button03"
  14. android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
  15. </LinearLayout>
  16. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/counter" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Count: 0" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="start" android:id="@+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0"></Button>
<Button android:text="stop" android:id="@+id/Button02"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:enabled="false"></Button>
<Button android:text="reset" android:id="@+id/Button03"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>

MyHandler.java

  1. package com.scnu.mc.myhandler;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. publicclass MyHandler extends Activity {
  10. private Button btnStart;
  11. private Button btnStop;
  12. private Button btnReset;
  13. private TextView tvCounter;
  14. privatelong count = 0;
  15. privateboolean run = false;
  16. privatefinal Handler handler = new Handler();
  17. privatefinal Runnable task = new Runnable() {
  18. @Override
  19. publicvoid run() {
  20. // TODO Auto-generated method stub
  21. if (run) {
  22. handler.postDelayed(this, 1000);
  23. count++;
  24. }
  25. tvCounter.setText("Count: " + count);
  26. }
  27. };
  28. /** Called when the activity is first created. */
  29. @Override
  30. publicvoid onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. btnStart = (Button) findViewById(R.id.Button01);
  34. btnStop = (Button) findViewById(R.id.Button02);
  35. btnReset = (Button) findViewById(R.id.Button03);
  36. tvCounter = (TextView) findViewById(R.id.counter);
  37. btnStart.setOnClickListener(new OnClickListener() {
  38. @Override
  39. publicvoid onClick(View v) {
  40. // TODO Auto-generated method stub
  41. run = true;
  42. updateButton();
  43. handler.postDelayed(task, 1000);
  44. }
  45. });
  46. btnStop.setOnClickListener(new OnClickListener() {
  47. @Override
  48. publicvoid onClick(View v) {
  49. // TODO Auto-generated method stub
  50. run = false;
  51. updateButton();
  52. handler.post(task);
  53. }
  54. });
  55. btnReset.setOnClickListener(new OnClickListener() {
  56. @Override
  57. publicvoid onClick(View v) {
  58. // TODO Auto-generated method stub
  59. count = 0;
  60. run = false;
  61. updateButton();
  62. handler.post(task);
  63. }
  64. });
  65. }
  66. privatevoid updateButton() {
  67. btnStart.setEnabled(!run);
  68. btnStop.setEnabled(run);
  69. }
  70. }
package com.scnu.mc.myhandler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MyHandler extends Activity {
private Button btnStart;
private Button btnStop;
private Button btnReset;
private TextView tvCounter;
private long count = 0;
private boolean run = false; private final Handler handler = new Handler(); private final Runnable task = new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
if (run) {
handler.postDelayed(this, 1000);
count++;
}
tvCounter.setText("Count: " + count);
}
}; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btnStart = (Button) findViewById(R.id.Button01);
btnStop = (Button) findViewById(R.id.Button02);
btnReset = (Button) findViewById(R.id.Button03);
tvCounter = (TextView) findViewById(R.id.counter); btnStart.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
run = true;
updateButton();
handler.postDelayed(task, 1000);
}
}); btnStop.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
run = false;
updateButton();
handler.post(task);
}
}); btnReset.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
count = 0;
run = false;
updateButton();
handler.post(task);
}
});
} private void updateButton() {
btnStart.setEnabled(!run);
btnStop.setEnabled(run);
}
}

[转]Android定时刷新UI界面----Handler的更多相关文章

  1. WPF定时刷新UI界面

    代码: using NHibernate.Criterion; using System; using System.Collections.Generic; using System.Collect ...

  2. Android 子线程无法刷新UI界面

    问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...

  3. Flutter获取远程数据 刷新UI界面

    import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() => r ...

  4. 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度

    BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...

  5. winform刷新UI界面

    this.Invoke(new Action(() => { // 更新使用次数 this.labCount.Text = count; }));

  6. Android研究之动态创建UI界面具体解释

     Android的基本UI界面一般都是在xml文件里定义好,然后通过activity的setContentView来显示在界面上.这是Android UI的最简单的构建方式.事实上,为了实现更加复 ...

  7. 线程操作UI界面的方法

    以前一般都是用BeginInvoke来刷新UI界面,现在采用 SynchronizationContext 来刷新,写起来清楚多了. SynchronizationContext synchroniz ...

  8. Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面

    Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...

  9. Android Handler传递参数动态更新UI界面demo

    package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...

随机推荐

  1. LINUX应用开发工程师职位(含答案)

    就业模拟测试题-LINUX应用开发工程师职位 本试卷从考试酷examcoo网站导出,文件格式为mht,请用WORD/WPS打开,并另存为doc/docx格式后再使用 试卷编号:143989试卷录入者: ...

  2. lua排序算法

    SEED = ; --随机序列 可任取 NUM = ; --排序规模 --随机序列 初始数据 function GenRnd( seed, n ) --生成随机数 data = {}; local r ...

  3. [模拟赛FJOI Easy Round #2][T1 sign] (模拟+求字符串重复字串)

    [题目描述] 小Z在无意中发现了一个神奇的OJ,这个OJ有一个神奇的功能:每日签到,并且会通过某种玄学的算法计算出今日的运势.在多次试验之后,小Z发现自己的运势按照一定的周期循环,现在他找到了你,请通 ...

  4. Vue2.0如何实现父组件与子组件之间的事件发射与接收

    关于vue2.0的事件发射和接收,大家都知道$dispatch和$broadcast在vue2.0已经被弃用了,取而代之的是更加方便快捷的方式,使用事件中心,组件通过它来互相通信,不管组件在哪一个层都 ...

  5. spring boot 的使用(一)

    1.  启动spring-boot项目 mvn spring-boot:run cd target java -jar xxxx.jar xxxx代表生成的jar包

  6. BAT、网易、京东等如何做大数据风控的?

    大数据风控目前应该是前沿技术在金融领域的最成熟应用,相对于智能投顾.区块链等还在初期的金融科技应用,大数据风控目前已经在业界逐步普及,从BATJ这样的大企业,到交易规模比较大的网贷平台,再到做现金贷. ...

  7. 使用spring框架提供的处理中文乱码的过滤器

    在web-xml中直接写上spring框架提供的过滤器即可 <filter> <filter-name>encoding</filter-name> <fil ...

  8. 非常适合新手的jq/zepto源码分析04

    $.extend = function(target){ var deep, args = slice.call(arguments, 1) if (typeof target == 'boolean ...

  9. oracle 12c 初步操作

    查看是否为cdb SQL> select name,cdb,open_mode,con_id from v$database; NAME CDB OPEN_MODE CON_ID ------- ...

  10. Docker: Docker容器之间互相通信

    最简单的方法,关闭防火墙(只建议用于开发环境) systemctl stop firewalld