【问题标题】:how to make digital clock display only hours and minutes如何使数字时钟只显示小时和分钟
【发布时间】:2012-06-24 00:10:05
【问题描述】:

我的应用中有一个活动的数字时钟,我只想在上面显示小时和分钟,不想显示秒。

【问题讨论】:

标签: android clock digital hour minute


【解决方案1】:

请在此处查看此答案,据我所知,您正在寻找类似/相同的东西:

Android: DigitalClock remove seconds

答案的主要内容如下:

类似于 AnalogClock,但是是数字的。显示秒。 FIXME:实施 小时/分钟/秒的单独视图,因此比例字体不会 抖动渲染。

根据 FIXME 的判断,隐藏部分 DigitalClock 的功能最终可能会实现。我什么也没找到 目前在 Javadoc 或源代码中,可以做你想做的事 到。 除非您想编写自己的扩展 DigitalClock 的类(或您自己的时钟实现),否则您可以 只需用另一个覆盖 DigitalClock 的秒部分 元素,如果它符合您的目的。

【讨论】:

    【解决方案2】:
    import android.content.Context;
    import android.database.ContentObserver;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.provider.Settings;
    import android.text.format.DateFormat;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    import java.util.Calendar;
    
    public class DigitalClock extends TextView {
    
        Calendar mCalendar;
        private final static String m24 = "k:mm";
        private FormatChangeObserver mFormatChangeObserver;
    
        private Runnable mTicker;
        private Handler mHandler;
    
        private boolean mTickerStopped = false;
    
        String mFormat;
    
        public DigitalClock(Context context) {
            super(context);
            initClock(context);
        }
    
        public DigitalClock(Context context, AttributeSet attrs) {
            super(context, attrs);
            initClock(context);
        }
    
        private void initClock(Context context) {
            if (mCalendar == null) {
                mCalendar = Calendar.getInstance();
            }
    
            mFormatChangeObserver = new FormatChangeObserver();
            getContext().getContentResolver().registerContentObserver(
                    Settings.System.CONTENT_URI, true, mFormatChangeObserver);
    
            setFormat();
        }
    
        @Override
        protected void onAttachedToWindow() {
            mTickerStopped = false;
            super.onAttachedToWindow();
            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();
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            mTickerStopped = true;
        }
    
    
        private void setFormat() {
                mFormat = m24;
    
        }
    
        private class FormatChangeObserver extends ContentObserver {
            public FormatChangeObserver() {
                super(new Handler());
            }
    
            @Override
            public void onChange(boolean selfChange) {
                setFormat();
            }
        }
    }
    

    您可以使用此自定义视图。这也适用于姜饼。根据自己的喜好更改时间格式。(m24)

    【讨论】:

      【解决方案3】:

      系统根据系统时钟在每分钟的确切开始发送广播事件。最可靠的方法是这样做:

      BroadcastReceiver _broadcastReceiver;
      private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm");
      private TextView _tvTime;
      
      @Override
      public void onStart() {
          super.onStart();
          _broadcastReceiver = new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context ctx, Intent intent) {
                      if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
                          _tvTime.setText(_sdfWatchTime.format(new Date()));
                  }
              };
      
          registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
      }
      
      @Override
      public void onStop() {
          super.onStop();
          if (_broadcastReceiver != null)
              unregisterReceiver(_broadcastReceiver);
      }
      

      但是不要忘记预先初始化您的 TextView(到当前系统时间),因为您很可能会在一分钟的中间弹出您的 UI,并且 TextView 直到下一分钟才会更新。

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 2017-10-15
        • 1970-01-01
        • 2023-02-16
        • 1970-01-01
        相关资源
        最近更新 更多