Android regularly refreshes the UI-Handler

Source: Internet
Author: User

If you want to create a software, you can regularly update the UI interface, find some information, and first paste a simple timer update interface program, which can increase the counter every one second.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.