阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1420|回复: 0

[转帖] Android 各种时间样式的设置方法

[复制链接]
  • TA的每日心情
    开心
    2012-3-7 10:15
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2012-2-4 11:47:42 | 显示全部楼层 |阅读模式
         本例中用到了三个时间样式,分别是模拟时钟样式,线程时钟样式和数字时钟样式
           三种时钟样式都是平时我们大家见到的显示时间的样式,所以希望这个帖子对大家有帮助,
    一  :本例基于android 2.2平台,都交代清楚了,现在开始做
        首先在layout布局里面修改main文件,需要添加两个TextView(算上默认的一共三个)  一个DigitalClock,一个AnalogClock,采用的是绝对布局,具体怎么添加相信大家都会,我就把代码贴上,以供参考:
    ..\res\layout\main.xml
       <?xml version="1.0" encoding="utf-8"?>
       <AbsoluteLayout
      android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView  
       android:id="@+id/TextView_showTime"
        android:layout_width="210px"
        android:layout_height="49px"
        android:textSize="35px"
        android:layout_x="92px"
        android:layout_y="232px"
       
        ></TextView>
    <AnalogClock
    android:id="@+id/Clock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="92px"
    android:layout_y="44px"></AnalogClock>

    <DigitalClock
    android:id="@+id/DigitalClock01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="35px"
    android:layout_x="92px"
    android:layout_y="300px"></DigitalClock>
    <TextView
    android:id="@+id/widget46"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="模拟时钟"
    android:layout_x="21px"
    android:layout_y="56px"


    ></TextView>
    <TextView
    android:id="@+id/widget47"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="线程时钟"
    android:layout_x="21px"
    android:layout_y="238px"
    ></TextView>
      <TextView
    android:id="@+id/widget48"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="数字时钟"
    android:layout_x="21px"
    android:layout_y="318px"
    ></TextView></AbsoluteLayout>
    二:开始写主程序了,为了直观的让大家理解 ,关键代码有注释,就不多废话了 ,贴上代码以供参考:
    package lhj.example.android.time;
    //导入需要的包
    import android.app.Activity;
    import android.os.Bundle;
    import java.util.Calendar;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.AnalogClock;
    import android.widget.DigitalClock;
    import android.widget.TextView;

    public class time extends Activity implements Runnable{
    //定义要使用的类对象
    private TextView showTime;//显示进程时钟的TextView
    AnalogClock myClock;//模拟时钟
    DigitalClock myDigClock;//数字时钟
    private final int msg_Key=0x1234;//发送消息内容
    public Handler myHandler;//发送处理消息的类
    public Calendar myCalendar;//日历类
    private int my_Hour,my_Minute,my_Second;//时分秒
    private Thread myT;


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //导入主屏布局main.xml
            setContentView(R.layout.main);
            //从xml中获取模拟字时钟UI对象
            myClock=(AnalogClock)findViewById(R.id.Clock);
            //从xml中获取数字字时钟UI对象
            myDigClock=(DigitalClock)findViewById(R.id.DigitalClock01);
           //从xml中获取TextView UI对象
            showTime=(TextView)findViewById(R.id.TextView_showTime);
            //通过Handler来接收进程所传递的信息并更新TextView
            myHandler=new Handler(){
             @Override
             public void handleMessage(Message msg){
              //这是处理信息的反法
              super.handleMessage(msg);
              switch (msg.what){
              case msg_Key:
               //在这处理要TextView对象Show时间事件
               showTime.setText(my_Hour+":"+my_Minute+":"+my_Second);
               break;
               default:break;
              }
             
             }
             
             
            };
            //通过进程来持续取得系统时间
            myT=new Thread(this);
            
            myT.start();
            
        }
    //实现一个Runable接口  实例化一个进程对象,用来持续的取得系统时间
    @Override
    public void run() {
      // TODO Auto-generated method stub
      try{
       do{
        //取得系统时间
        long Time=System.currentTimeMillis();
        myCalendar=Calendar.getInstance();
        myCalendar.setTimeInMillis(Time);
        my_Hour=myCalendar.get(Calendar.HOUR);
        my_Minute=myCalendar.get(Calendar.MINUTE);
        my_Second=myCalendar.get(Calendar.SECOND);
        //让进程休息一秒
        Thread.sleep(1000) ;
        //重要关键程序:取出时间后发出信息给Handler
        Message msg=new Message();
        msg.what=msg_Key;
        myHandler.sendMessage(msg);
        //重要关键程序   取得时间后发送信息给Handler
       }while(myT.interrupted()==false);
       
       //当系统发出中断信息时停止本循环
       }catch(InterruptedException e){
        e.printStackTrace();
      }
      
    }
    }

    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表