Android Clock控件

1. AnalogClock模拟时钟

属性配置

  • dial,表盘背景
  • hand_hour,时针图片
  • hand_minute,秒针图片

配置文件

<AnalogClock
    android:layout_width="100dp"
    android:layout_height="100dp" />

<AnalogClock
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:dial="@drawable/analog_clock_dial"
    android:hand_minute="@drawable/analog_clock_minute"
    android:hand_hour="@drawable/analog_clock_hour" />

效果如下
在这里插入图片描述
AnalogClock添加了对时间事件的监听

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    if (!mAttached) {
        mAttached = true;
        IntentFilter filter = new IntentFilter();

        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

        // OK, this is gross but needed. This class is supported by the
        // remote views machanism and as a part of that the remote views
        // can be inflated by a context for another user without the app
        // having interact users permission - just for loading resources.
        // For exmaple, when adding widgets from a user profile to the
        // home screen. Therefore, we register the receiver as the current
        // user not the one the context is for.
        getContext().registerReceiverAsUser(mIntentReceiver,
                android.os.Process.myUserHandle(), filter, null, getHandler());
    }

    // NOTE: It's safe to do these after registering the receiver since the receiver always runs
    // in the main thread, therefore the receiver can't run before this method returns.

    // The time zone may have changed while the receiver wasn't registered, so update the Time
    mCalendar = new Time();

    // Make sure we update to the current time
    onTimeChanged();
}

private void onTimeChanged() {
    mCalendar.setToNow();

    int hour = mCalendar.hour;
    int minute = mCalendar.minute;
    int second = mCalendar.second;

    mMinutes = minute + second / 60.0f;
    mHour = hour + mMinutes / 60.0f;
    mChanged = true;

    updateContentDescription(mCalendar);
}

2. DigitalClock数字时钟

DigitalClock显示默认的时间格式,如果想修改,可以修改mFormat属性

DigitalClock clock = findViewById(R.id.digital_clock);
Class cls = clock.getClass();
try {
    Field f = cls.getDeclaredField("mFormat");
    f.setAccessible(true);
    f.set(clock, "yyyy-MM-dd hh:mm");
} catch (Exception e) {
}

效果如下
在这里插入图片描述
DigitalClock使用线程修改时间

@Override
protected void onAttachedToWindow() {
    mTickerStopped = false;
    super.onAttachedToWindow();

    mFormatChangeObserver = new FormatChangeObserver();
    getContext().getContentResolver().registerContentObserver(
            Settings.System.CONTENT_URI, true, mFormatChangeObserver);
    setFormat();

    mHandler = new Handler();

    /**
     * requests a tick on the next hard-second boundary
     */
    mTicker = new Runnable() {
        public void run() {
            if (mTickerStopped) return;
            mCalendar.setTimeInMillis(System.currentTimeMillis());
            setText(DateFormat.format(mFormat, mCalendar));
            invalidate();
            long now = SystemClock.uptimeMillis();
            long next = now + (1000 - now % 1000);
            mHandler.postAtTime(mTicker, next);
        }
    };
    mTicker.run();
}

3. TextClock数字时钟

属性配置

  • format12Hour,12时制格式
  • format24Hour,24时制格式

DigitalClock继承TextView类,可以设置textSizetextColor等属性。并且根据系统配置显示时间格式。

<TextClock
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:format12Hour="yyyy-MM-dd hh:mm aa"
    android:format24Hour="yyyy-MM-dd HH:mm"
    android:textSize="15sp"
    android:textColor="@color/red"/>

<TextClock
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:format12Hour="yyyy-MM-dd hh:mm aa"
    android:format24Hour="yyyy-MM-dd HH:mm"
    android:textSize="12sp"
    android:textColor="@color/blue"/>

24进制效果如下
在这里插入图片描述
12进制效果如下
在这里插入图片描述
TextClock同样添加了对时间事件的监听

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    if (!mRegistered) {
        mRegistered = true;

        registerReceiver();
        registerObserver();

        createTime(mTimeZone);
    }
}

private void registerReceiver() {
    final IntentFilter filter = new IntentFilter();

    filter.addAction(Intent.ACTION_TIME_TICK);
    filter.addAction(Intent.ACTION_TIME_CHANGED);
    filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

    // OK, this is gross but needed. This class is supported by the
    // remote views mechanism and as a part of that the remote views
    // can be inflated by a context for another user without the app
    // having interact users permission - just for loading resources.
    // For example, when adding widgets from a managed profile to the
    // home screen. Therefore, we register the receiver as the user
    // the app is running as not the one the context is for.
    getContext().registerReceiverAsUser(mIntentReceiver, android.os.Process.myUserHandle(),
            filter, null, getHandler());
}

相关文章
Android Picker控件
Android Clock控件
Android Chronometer控件
Android SystemClock类
Java Calendar类
Java SimpleDateFormat类

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android的Chronometer控件可以用于实现计时器功能,包括正计时和倒计时。如果想要实现倒计时,需要在代码中设置Chronometer控件的计时时间,并在倒计时结束时触发相关操作。 具体实现方式如下: 1. 在布局文件中添加Chronometer控件: ``` <Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:format="倒计时:%s" android:layout_centerInParent="true"/> ``` 其中,format属性用于设置Chronometer控件显示的文本格式,%s表示计时器的时间将会被替换为具体的倒计时时间。 2. 在代码中设置倒计时时间: ``` Chronometer chronometer = findViewById(R.id.chronometer); chronometer.setBase(SystemClock.elapsedRealtime() + 10 * 1000); //设置倒计时时间为10秒 chronometer.start(); //开始倒计时 ``` 其中,setBase方法用于设置Chronometer控件的起始时间,这里使用SystemClock.elapsedRealtime()获取当前时间,再加上10秒的时间作为起始时间,即表示倒计时从当前时间开始,倒计时时间为10秒。start方法用于开始倒计时。 3. 在倒计时结束时触发相关操作: ``` chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { if (chronometer.getText().toString().equals("00:00")) { //倒计时结束,执行相关操作 chronometer.stop(); //停止倒计时 } } }); ``` 在倒计时过程中,可以通过设置setOnChronometerTickListener监听器,在每秒钟的时钟周期中判断Chronometer控件显示的时间是否为00:00,如果是则表示倒计时结束,可以在这里触发相关操作。同时,需要在倒计时结束时停止Chronometer的计时器,避免继续计时。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值