本文转自: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);
}
}

最新文章

  1. HTML5教程之html 5 本地数据库(Web Sql Database)
  2. [原创]Centos7 从零编译Nginx+PHP+MySql
  3. 清华学堂 列车调度(Train)
  4. sql语句判断默认值为getdate()的约束是否存在
  5. POJ1185炮兵阵地(状态压缩 + dp)
  6. 20145212《Java程序程序设计》课程总结
  7. s3c2440 移值u-boot-2016.03 第2篇 支持Nand flash启动
  8. 快速集成iOS基于RTMP的视频推流
  9. 点击次数(thinkphp)
  10. sql2005如何附加数据库
  11. XCODE 控件连接(关联)不上变量 怎么解决
  12. LVS+keepalived负载均衡
  13. Gedit —— 推荐于NOI系列考试(NOIlinux)的轻量编程环境
  14. 51nod 1009 数字1的数量(数位dp模板)
  15. 【软件安装与环境配置】ubuntu16.04+caffe+nvidia+CUDA+cuDNN安装配置
  16. Java学习图
  17. Maven 工程读取resource下的文件
  18. golang 通过fsnotify监控文件,并通过文件变化重启程序
  19. Android启动过程
  20. vue-cli的工程如何正确使用Google Analytics?

热门文章

  1. 洛谷 1472 奶牛家谱 Cow Pedigrees
  2. 使用lombok提高编码效率-----不用写get set
  3. Ubuntu16.04再次装机记
  4. 高数A(下)第九章
  5. uva 11552 dp
  6. ubuntu刪除升級后多余的内核
  7. org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframwork.web.mult..
  8. Chrome 消息机制
  9. 【转】 vsftp上传文件出现553 Could not create file解决方法
  10. error: &amp;#39;Can&amp;#39;t connect to local MySQL server through socket &amp;#39;/var/lib/mysql/mysql.sock&amp;#39; (2)&amp;#39;