HandlerTimer1

字型檔存放位置

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:gravity="center"
        android:text="20:47:00"
        android:textColor="#ffffff"
        android:textSize="70sp" />

</RelativeLayout>
package com.example.app21;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {

    Handler handler  = new Handler();
    TextView text;
    Calendar cal;
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) this.findViewById(R.id.textView1);
        handler.post(updateTask);

        Typeface tf = Typeface.createFromAsset(this.getAssets(), "Digital Dismay.otf");        
        text.setTypeface(tf);        
    }

    Runnable updateTask = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            cal= Calendar.getInstance();            
            text.setText(sdf.format(cal.getTime()));
            //Log.e("MainActivity", "現在的時間:" + sdf.format(cal.getTime()));

            handler.postDelayed(updateTask, 1000);
        }
    };
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        handler.removeCallbacks(updateTask);
        super.onStop();
    }
}