Android定時重新整理UI介面—-Handler

來源:互聯網
上載者:User

本想做一個軟體可以對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() {@Overridepublic void run() {// TODO Auto-generated method stubif (run) {handler.postDelayed(this, 1000);count++;}tvCounter.setText("Count: " + count);}};/** Called when the activity is first created. */@Overridepublic 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubrun = true;updateButton();handler.postDelayed(task, 1000);}});btnStop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubrun = false;updateButton();handler.post(task);}});btnReset.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcount = 0;run = false;updateButton();handler.post(task);}});}private void updateButton() {btnStart.setEnabled(!run);btnStop.setEnabled(run);}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.