package com.powenko.Tutorial_thread_Runnable; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class Tutorial_thread_RunnableActivity extends Activity { // Created by PowenKo.com on 2011/2/23. private TextView mTextView1; private Handler handler; private int mClock=0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView1=(TextView)findViewById(R.id.textView1); handler = new Handler(); handler.postDelayed(runner, 1000); } final Runnable runner = new Runnable() { public void run() { mClock++; mTextView1.setText("PowenKo.com \n Runnable timer:"+mClock); handler.postDelayed(this, 1000); } }; }
<?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/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
<h3>sample code:</h3>
Tutorial_thread_Runnable