Android定时器

来源:互联网 发布:乐视mac码 编辑:程序博客网 时间:2024/05/17 08:05
package com.time;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends Activity {    private int recLen = 10;    private Timer timer=new Timer();    private TextView time;    /**     * Called when the activity is first created.     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        time =(TextView)findViewById(R.id.time);        timer.schedule(task,1000,1000 );    }    TimerTask task = new TimerTask() {        @Override        public void run() {            runOnUiThread(new Runnable() {                @Override                public void run() {                    recLen--;                    time.setText(recLen+"");                    if(recLen<0){                        timer.cancel();                        time.setVisibility(View.INVISIBLE);                    }                }            });        }    };

}

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:gravity="center"              android:background="@android:color/white"              android:layout_height="fill_parent"  >    <TextView            android:id="@+id/time"            android:textSize="50sp"            android:textColor="@android:color/black"            android:layout_width="wrap_content"            android:textStyle="bold"            android:layout_height="wrap_content"            android:text="xxxx"      /></LinearLayout>


http://blog.csdn.net/leaning_wk/article/details/49492021

0 0
原创粉丝点击