博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实现自定义的 时间日期 控件
阅读量:6986 次
发布时间:2019-06-27

本文共 3990 字,大约阅读时间需要 13 分钟。

自定义DigitalClock的布局

 

 

 

布局空间设置

DigitalClock

 

package com.javen.digitalclock;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.TextView;public class DigitalClock extends LinearLayout {        static SimpleDateFormat sdf_time = new SimpleDateFormat("hh:mm:ss");        static SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy年MM月dd日");        static Calendar cal = Calendar.getInstance();        private TextView textViewTime, textViewDate, textViewWeek;//        public DigitalClock(Context context, AttributeSet attrs, int defStyle) {//                super(context, attrs, defStyle);//        }        public DigitalClock(Context context, AttributeSet attrs) {                super(context, attrs);                // 使用layoutinflater把布局加载到本ViewGroup                LayoutInflater inflater = (LayoutInflater) context                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);                inflater.inflate(R.layout.digitalcolck_layout, this);                textViewTime = (TextView) findViewById(R.id.textViewTime);                textViewDate = (TextView) findViewById(R.id.textViewDate);                textViewWeek = (TextView) findViewById(R.id.textViewWeek);                startThread();        }        public static String getCurrentTime(Date date) {                sdf_time.format(date);                return sdf_time.format(date);        }        public static String getCurrentDate(Date date) {                sdf_date.format(date);                return sdf_date.format(date);        }        public static String getCurrentWeekDay(Date dt) {                String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };                cal.setTime(dt);                int w = cal.get(Calendar.DAY_OF_WEEK) - 1;                if (w < 0)                        w = 0;                return weekDays[w];        }        private void startThread() {                new Thread(new Runnable() {                        public void run() {                                while (true) {                                        handler.sendEmptyMessage(12);                                        try {                                                Thread.sleep(1000);                                        } catch (InterruptedException e) {                                                // TODO Auto-generated catch block                                                e.printStackTrace();                                        }                                }                        }                }).start();        }        Handler handler = new Handler() {                public void handleMessage(Message msg) {                        super.handleMessage(msg);                        if (msg.what == 12) {                                Date date = new Date();                                textViewTime.setText(getCurrentTime(date));                                textViewDate.setText(getCurrentDate(date));                                textViewWeek.setText(getCurrentWeekDay(date));                        }                }        };}

 

mainActivity

 

package com.javen.digitalclock;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    @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;    }}

布局文件main.xml

 

 

转载地址:http://ijmpl.baihongyu.com/

你可能感兴趣的文章
巴特沃斯(Butterworth)滤波器 (2) - 双线性变换
查看>>
iOS 小知识点(持续更新)
查看>>
iOS人脸识别(CoreImage)
查看>>
java.net.SocketException: Software caused connection abort: socket write error
查看>>
MySQL 随机取数据效率问题
查看>>
ArcGIS for Desktop入门教程_第八章_Desktop学习资源 - ArcGIS知乎-新一代ArcGIS问答社区...
查看>>
根据id查询数据(向前台返回json格式的数据)
查看>>
JMeter中3种参数值的传递
查看>>
hive操作语句使用详解
查看>>
根据指定类型计算两个日期相差的时间
查看>>
【转】【Linux】linux awk命令详解
查看>>
EasyUI ---- draggable购物车
查看>>
Jdom读取XML文件
查看>>
Spring Boot 配置文件 – 在坑中实践
查看>>
研究技术心得
查看>>
在windows搭建jenkins測试环境
查看>>
Inspect a new tab · cyrus-and/chrome-remote-interface Wiki
查看>>
高中毕业,我想去看看-屌丝程序员的逆袭之旅
查看>>
【分片无法挂载】Elasticsearch分片和副本无法挂载(分片移位)
查看>>
免费创建微信公众号全攻略
查看>>