[转]Android定时刷新UI界面----Handler
本文转自:http://blog.csdn.net/macong01/article/details/7479266
本想做一个软件可以对UI界面进行定时更新,找了一些资料,先贴一个简单的定时更新界面程序,可以实现每隔1秒递增计数器的功能。
界面布局文件main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:id="@+id/counter"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="Count: 0"/>
- <LinearLayoutandroid:orientation="horizontal"
- android:layout_width="fill_parent"android:layout_height="wrap_content">
- <Buttonandroid:text="start"android:id="@+id/Button01"
- android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
- <Buttonandroid: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>
- <Buttonandroid:text="reset"android:id="@+id/Button03"
- android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"></Button>
- </LinearLayout>
- </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
- 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;
- publicclass MyHandler extends Activity {
- private Button btnStart;
- private Button btnStop;
- private Button btnReset;
- private TextView tvCounter;
- privatelong count = 0;
- privateboolean run = false;
- privatefinal Handler handler = new Handler();
- privatefinal Runnable task = new Runnable() {
- @Override
- publicvoid 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
- publicvoid 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
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- run = true;
- updateButton();
- handler.postDelayed(task, 1000);
- }
- });
- btnStop.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- run = false;
- updateButton();
- handler.post(task);
- }
- });
- btnReset.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- count = 0;
- run = false;
- updateButton();
- handler.post(task);
- }
- });
- }
- privatevoid updateButton() {
- btnStart.setEnabled(!run);
- btnStop.setEnabled(run);
- }
- }
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的更多相关文章
- WPF定时刷新UI界面
代码: using NHibernate.Criterion; using System; using System.Collections.Generic; using System.Collect ...
- Android 子线程无法刷新UI界面
问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...
- Flutter获取远程数据 刷新UI界面
import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() => r ...
- 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度
BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...
- winform刷新UI界面
this.Invoke(new Action(() => { // 更新使用次数 this.labCount.Text = count; }));
- Android研究之动态创建UI界面具体解释
Android的基本UI界面一般都是在xml文件里定义好,然后通过activity的setContentView来显示在界面上.这是Android UI的最简单的构建方式.事实上,为了实现更加复 ...
- 线程操作UI界面的方法
以前一般都是用BeginInvoke来刷新UI界面,现在采用 SynchronizationContext 来刷新,写起来清楚多了. SynchronizationContext synchroniz ...
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...
- Android Handler传递参数动态更新UI界面demo
package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...
随机推荐
- cmake 学习笔记(一) buildsystem
参见网址: http://www.cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html Introduction 基于CMake的构建 ...
- redis学习-简介
1. Redis 简介 1.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Red ...
- Hello Shiro
[HelloWorld Shiro] 1.搭建开发环境-加入jar包 2.步骤(前提:已下载好Shiro资源包): ①找到shiro-root-1.2.3-source-release包, ②按Apa ...
- 【04】AngularJS 表达式
AngularJS 表达式 AngularJS 使用 表达式 把数据绑定到 HTML. AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}. Ang ...
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 Scores
#1236 : Scores 时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 Kyle is a student of Programming Monkey Element ...
- Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)
Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...
- [NOIP2004]FBI树
题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三 ...
- POJ 1523 SPF 割点 Tarjan
SPF Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9317 Accepted: 4218 Description C ...
- C. Painting Fence 分治
memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...
- 30、Java并发性和多线程-阿姆达尔定律
以下内容转自http://ifeve.com/amdahls-law/: 阿姆达尔定律可以用来计算处理器平行运算之后效率提升的能力.阿姆达尔定律因Gene Amdal 在1967年提出这个定律而得名. ...