现在的位置: 首页 > 综合 > 正文

andriod 中实现一个钟表的实例

2013年11月26日 ⁄ 综合 ⁄ 共 2670字 ⁄ 字号 评论关闭

基本上没什么技术含量。

java 代码

import java.util.Calendar;
import android.os.Handler;
import android.os.Message;
import android.widget.*;
import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
 static final int CUTTIME=0x1234;
  static Handler myHandler;
 public TextView tv;
 public Calendar myCalendar;
 public int minTime=10;
 public int mHour=10;
 private Thread myThreadl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv=(TextView)findViewById(R.id.view1);
        AnalogClock ac=(AnalogClock)findViewById(R.id.analogClock1);
        myThreadl=new LooperThread();
        myThreadl.start();
        myHandler= new Handler()
        {
         public void handleMessage(Message msg)
         {
          switch(msg.what)
          {
          case CUTTIME:
           tv.setText(mHour+":"+minTime);
           break;
          }
          super.handleMessage(msg);
         }
        };
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
    class LooperThread extends Thread{

   public void run()
   {
    super.run();
    try{
     do{
      long time=System.currentTimeMillis();
      final Calendar  calendar=Calendar.getInstance();
      calendar.setTimeInMillis(time);
      minTime=calendar.get(Calendar.MINUTE);
      mHour=calendar.get(Calendar.HOUR);
      Thread.sleep(1000);
      Message m=new Message();
      m.what=MainActivity.CUTTIME;
      //Handler myHandler;
      MainActivity.myHandler.sendMessage(m);
     }while(LooperThread.interrupted()==false);
     }
    catch(Exception e)
    {
     e.printStackTrace();
    }
    
    }
   }
   
}

xml代码

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp" />

   <TextView
       android:id="@+id/view1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/analogClock1" />

</RelativeLayout>

 

抱歉!评论已关闭.