我的应用程序中有一个活动的数字时钟,我只想在上面显示小时和分钟,不想显示秒。
问问题
5138 次
3 回答
2
请在此处查看此答案,据我所知,您正在寻找类似/相同的东西:
回答的主要内容如下:
像 AnalogClock,但是是数字的。显示秒。FIXME:实现小时/分钟/秒的单独视图,因此比例字体不会影响渲染。
从 FIXME 来看,隐藏部分 DigitalClock 的能力最终可能会实现。我目前在 Javadoc 或源代码中没有找到任何可以执行您想要的操作的内容。除非您想编写自己的类来扩展 DigitalClock(或您自己的时钟实现),否则您可以只用另一个元素覆盖 DigitalClock 的秒部分,如果它符合您的目的。
于 2012-06-21T17:36:21.370 回答
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(到当前系统时间),因为您可能会在一分钟的中间弹出您的 UI,并且 TextView 直到下一分钟才会更新。
于 2013-07-14T19:20:01.760 回答
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)
于 2014-06-09T09:26:36.773 回答