2012-06-21 144 views
1

我有一个数字时钟在我的应用程序的活动,我想只显示小时和分钟,并不想显示秒。如何使数字时钟只显示小时和分钟

+0

代码plz ....... –

+0

可能出现[Android:DigitalClock remove seconds]重复(http://stackoverflow.com/questions/7610549/android-digitalclock) -remove - 秒) – Veger

回答

2

请看一看这个答案在这里,而据我可以看到你正在寻找一个类似/同样的事情:

Android: DigitalClock remove seconds

答案的主要内容如下:

像AnalogClock,但数字。显示秒。 FIXME:实现 小时/分钟/秒的单独视图,所以比例字体不会抖动渲染。

通过FIXME判断,最终可以隐藏DigitalClock的部分功能。我没有找到任何目前在Javadoc或源代码中的任何 ,它们会执行你想要的任务 。 除非你想编写自己的类来扩展DigitalClock(或者你自己的时钟实现),否则你可能只需要覆盖DigitalClock的秒部分和另一个 元素,如果它符合你的目的。

1

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

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(为当前系统时间),因为很可能你会弹出一分钟的中间你的用户界面和TextView将不会更新,直到下一分钟发生。

1
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)