我想从数字时钟小部件中删除秒和 AM/PM 指示,但我不知道该怎么做。
问问题
3390 次
3 回答
8
它在 DigitalClock 类中是硬编码的,因此您不能以编程方式对其进行更改。改变它的唯一方法是重新实现 DigitalClock 小部件。哟可以从这里获取源代码。
您需要将此代码复制到您的项目中,将其重命名为“MyDigitalClock”并将其用作您的小部件。正如您将在实现中看到的那样,您应该关注两个变量以获得您想要的:
private final static String m12 = "h:mm:ss aa";
private final static String m24 = "k:mm:ss";
ss 代表秒,aa 代表上午/下午。
于 2012-07-06T16:43:10.777 回答
2
您可以使用 addTextChangedListener(),然后在 afterTextChanged() 中取出秒数和 AM/PM。此示例仅删除秒数:
DigitalClock dc = (DigitalClock) findViewById(R.id.currentTime);
dc.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.charAt(4) == ':') {
s.delete(4, 7);
} else if (s.charAt(5) == ':') {
s.delete(5, 8);
}
}
});
于 2013-07-18T00:39:36.907 回答
1
让我一点点扩展和刷新Erol的完美答案。
实际上,您需要在项目中添加一个文件并更新 xml 布局。
可以从这里获取示例:/frameworks/base/core/java/android/widget/DigitalClock.java
改编的 DigitalClock.java:
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.text.SimpleDateFormat;
import java.util.Calendar;
/**
* Like AnalogClock, but digital. Shows seconds.
*
* @deprecated It is recommended you use {@link TextClock} instead.
*/
@Deprecated
public class DigitalClock extends TextView {
// FIXME: implement separate views for hours/minutes/seconds, so
// proportional fonts don't shake rendering
Calendar mCalendar;
@SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer
private FormatChangeObserver mFormatChangeObserver;
private Runnable mTicker;
private Handler mHandler;
private boolean mTickerStopped = false;
java.text.DateFormat mFormat;
public DigitalClock(Context context) {
super(context);
initClock();
}
public DigitalClock(Context context, AttributeSet attrs) {
super(context, attrs);
initClock();
}
private void initClock() {
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));
setText(mFormat.format(mCalendar.getTime()));
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 = DateFormat.getTimeFormatString(getContext()); //<== ORIGINAL
// mFormat = DateFormat.getTimeFormat(getContext()); //<== Fixed with PM
// mFormat = new SimpleDateFormat("hh:mm a");
mFormat = new SimpleDateFormat("h:mm"); //<== Your variant
}
private class FormatChangeObserver extends ContentObserver {
public FormatChangeObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
setFormat();
}
}
@Override
public CharSequence getAccessibilityClassName() {
//noinspection deprecation
return DigitalClock.class.getName();
}
}
布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity" >
<HERE_YOUR_PACKAGE_NAME.DigitalClock
android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:layout_centerInParent="true"
android:textSize="130dp"/>
</RelativeLayout>
结果:
于 2017-02-10T00:48:27.980 回答