2012-06-21 148 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)