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

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。