Android定时刷新UI界面----Handler

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

界面布局文件main.xml

<?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;

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);
	}
}


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值