Android实现自定义的 时间日期 控件

本文介绍了一种自定义的数字时钟控件实现方法,该控件用于显示当前时间、日期及星期,并通过一个独立线程每分钟更新一次显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 自带的时钟效果太差 于是自定义一个控件 效果



使用起来和使用自带空间差不多 main.xml布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	tools:context=".MainActivity"
	android:layout_width="match_parent"
	android:layout_height="match_parent" >
	<com.qiho.ecoach.uiwidget.DigitalClock
		android:id="@+id/digitalClock1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="DigitalClock" />
</RelativeLayout>
activity什么也没有就不写了


下面介绍一下闹钟控件 java代码 放到 随便一个包里注意和上面一致

package com.qiho.ecoach.uiwidget;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiho.ecoach.R;

public class DigitalClock extends LinearLayout {

	static SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm");
	static SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy年MM月dd日");
	static Calendar cal = Calendar.getInstance();
	private View view;
	private TextView textViewTime, textViewDate, textViewWeek;

	public DigitalClock(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	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 * 60);
					} 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));
			}

		}
	};

}

layout文件 现在唯一不好的地方就是多了一个layout文件 个人比较懒惰 你可以这个布局在java中实现:注意要分享喔~

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:gravity="center" >
	<TextView
		android:id="@+id/textViewTime"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="55sp"
		android:text="10:50"
		android:layout_gravity="center"
		android:lines="1" />
	<LinearLayout
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:orientation="vertical"
		android:layout_gravity="center"
		android:paddingLeft="5dip" >
		<TextView
			android:id="@+id/textViewWeek"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:textSize="20sp"
			android:text="星期五"
			android:lines="1" />
		<TextView
			android:id="@+id/textViewDate"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:textSize="20sp"
			android:text="2013年12月1日"
			android:lines="1" />
	</LinearLayout>
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值