[Android開發]AnalogClock和DigitalClock的使用

來源:互聯網
上載者:User

DigitalClock直接放到Layout中就可以使用,不需要實現什麼代碼。

主要看看AnaLogClock和TextView(用於類比DigitalClock)的結合使用。

package com.example;

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.AnalogClock;
import android.widget.TextView;

public class EX0414 extends Activity {
    /** Called when the activity is first created. */
 //聲明一常數作為判別資訊用
 protected static final int GUINOTIFIER = 0x1234;
 
 //聲明兩個widget物件變數
 private TextView mTextView;
 public AnalogClock mAnalogClock;
 //聲明與時間相關的變數
 public Calendar mCalendar;
 public int mMinutes;
 public int mHour;
 //聲明關鍵Handler與Thread變數
 public Handler mHandler;
 private Thread mClockThread;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView =(TextView)findViewById(R.id.myTextView);
        mAnalogClock=(AnalogClock)findViewById(R.id.myAnalogClock);
       
        //通過Handler來接收進程所傳遞的資訊並進行更新TextView
        mClockThread = new LooperThread();
        mClockThread.start();
        mHandler = new Handler()
        {
         public void handleMessage(Message msg)
            {
          /*處理資訊方法*/
              switch (msg.what)
               {
                 case EX0414.GUINOTIFIER:
                  /*在這處理要TextView對象Show時間的事件*/       
                   mTextView.setText(mHour+" : "+mMinutes);
                   break;
               }
               super.handleMessage(msg);
            }
        };
        //通過進程來持續取得系統時間
      
    }
    class LooperThread extends Thread
    {
     public void run()
     {
      super.run();
      try
            {
             do
             {
              /*取得系統時間*/
              long time = System.currentTimeMillis();
                 final Calendar mCalendar = Calendar.getInstance();
               mCalendar.setTimeInMillis(time);
               mHour = mCalendar.get(Calendar.HOUR);
               mMinutes = mCalendar.get(Calendar.MINUTE);
               Thread.sleep(1000);
               Message m = new Message();
               m.what = EX0414.GUINOTIFIER;
               EX0414.this.mHandler.sendMessage(m);      
             }while(EX0414.LooperThread.interrupted()==false);
            }
            catch(Exception e)
            {
             e.printStackTrace();
            }
     }
    }
}

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"
    >
<AnalogClock
  android:id="@+id/myAnalogClock"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
>
</AnalogClock>
<TextView
  android:id="@+id/myTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="TextView"
  android:textSize="20sp"
  android:layout_gravity="center_horizontal"
>
</TextView>
<DigitalClock
 android:id="@+id/digitalClock1"
 android:text="DigitalClock"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 >
 </DigitalClock>
</LinearLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.