下面列出了怎么用android.widget.TextClock的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
SharedPreferences prefs = getSharedPreferences(getString(R.string.setting_header),
Context.MODE_PRIVATE);
mClock12hour = prefs.getBoolean(getString(R.string.setting_12hour), true);
TimeZone tz = TimeZone.getTimeZone(getString(R.string.pst_time_zone));
TextClock tc = (TextClock) findViewById(R.id.text_clock);
TextView tv = (TextView) findViewById(R.id.clock_format);
if (mClock12hour) {
tc.setFormat12Hour(getString(R.string.format_12hour));
tv.setText(getString(R.string.setting_12hour));
} else {
tc.setFormat12Hour(getString(R.string.format_24hour));
tv.setText(getString(R.string.setting_24hour));
}
tc.setTimeZone(tz.getID());
}
public void run() {
final View parent = (View) getParent();
if (parent == null) return;
// reposition in parent using setX() and setY()
final float width = getMeasuredWidth();
final float height = getMeasuredHeight();
final float parentw = parent.getMeasuredWidth();
final float parenth = parent.getMeasuredHeight();
setX((float) Math.random() * width + (parentw/2 - width));
setY((float) Math.random() * height + (parenth/2 - height));
Random rand = new Random();
// generate the random integers for r, g and b value
int r = rand.nextInt(COLOR_MAX);
int g = rand.nextInt(COLOR_MAX);
int b = rand.nextInt(COLOR_MAX);
int randomColor = Color.rgb(r, g, b);
TextClock tc = (TextClock) findViewById(R.id.text_clock);
tc.setTextColor(randomColor);
postDelayed(this, DRIFT_DELAY); // let’s do this again, soon
}
private void init(Context context) {
this.context = context;
inflate(context, R.layout.view_time_widget, this);
clock = (TextClock) findViewById(R.id.text_clock);
date = (TextView) findViewById(R.id.date);
setVisibility(INVISIBLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_face);
mBackground = (ImageView) findViewById( R.id.watch_background );
mContainer = (LinearLayout) findViewById( R.id.watch_container );
mClock = (TextClock) findViewById( R.id.watch_time );
}
@SuppressLint({"SetTextI18n", "InflateParams"})
public FirstCardHeaderController(@NonNull GeoActivity activity, @NonNull Location location) {
this.activity = activity;
this.view = LayoutInflater.from(activity).inflate(R.layout.container_main_first_card_header, null);
AppCompatImageView timeIcon = view.findViewById(R.id.container_main_first_card_header_timeIcon);
TextView refreshTime = view.findViewById(R.id.container_main_first_card_header_timeText);
TextClock localTime = view.findViewById(R.id.container_main_first_card_header_localTimeText);
TextView alert = view.findViewById(R.id.container_main_first_card_header_alert);
View line = view.findViewById(R.id.container_main_first_card_header_line);
ThemeManager themeManager = ThemeManager.getInstance(activity);
if (location.getWeather() != null) {
this.weather = location.getWeather();
view.setOnClickListener(v ->
IntentHelper.startManageActivityForResult(activity, MainActivity.MANAGE_ACTIVITY));
view.setEnabled(!MainDisplayUtils.isMultiFragmentEnabled(activity));
if (weather.getAlertList().size() == 0) {
timeIcon.setEnabled(false);
timeIcon.setImageResource(R.drawable.ic_time);
} else {
timeIcon.setEnabled(true);
timeIcon.setImageResource(R.drawable.ic_alert);
}
ImageViewCompat.setImageTintList(
timeIcon,
ColorStateList.valueOf(themeManager.getTextContentColor(activity))
);
timeIcon.setOnClickListener(this);
refreshTime.setText(
activity.getString(R.string.refresh_at)
+ " "
+ Base.getTime(activity, weather.getBase().getUpdateDate())
);
refreshTime.setTextColor(themeManager.getTextContentColor(activity));
long time = System.currentTimeMillis();
if (TimeZone.getDefault().getOffset(time) == location.getTimeZone().getOffset(time)) {
// same time zone.
localTime.setVisibility(View.GONE);
} else {
localTime.setVisibility(View.VISIBLE);
localTime.setTimeZone(location.getTimeZone().getID());
localTime.setTextColor(themeManager.getTextSubtitleColor(activity));
localTime.setFormat12Hour(
activity.getString(R.string.date_format_widget_long) + ", h:mm aa"
);
localTime.setFormat24Hour(
activity.getString(R.string.date_format_widget_long) + ", HH:mm"
);
}
if (weather.getAlertList().size() == 0) {
alert.setVisibility(View.GONE);
line.setVisibility(View.GONE);
} else {
alert.setVisibility(View.VISIBLE);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < weather.getAlertList().size(); i ++) {
builder.append(weather.getAlertList().get(i).getDescription())
.append(", ")
.append(
DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.DEFAULT
).format(weather.getAlertList().get(i).getDate())
);
if (i != weather.getAlertList().size() - 1) {
builder.append("\n");
}
}
alert.setText(builder.toString());
alert.setTextColor(themeManager.getTextSubtitleColor(activity));
line.setVisibility(View.VISIBLE);
line.setBackgroundColor(themeManager.getRootColor(activity));
}
alert.setOnClickListener(this);
}
}
public static BaseDSL.ViewClassResult textClock() {
return BaseDSL.v(TextClock.class);
}