Java 基于日期/时间的手表表面颜色
我正在尝试创建一个有am/PM油漆的手表,我想根据它是am还是PM来选择不同的颜色。我还想把它扩展到一周中的几天 SimpleWatchFace.javaJava 基于日期/时间的手表表面颜色,java,android,wear-os,Java,Android,Wear Os,我正在尝试创建一个有am/PM油漆的手表,我想根据它是am还是PM来选择不同的颜色。我还想把它扩展到一周中的几天 SimpleWatchFace.java Paint ampmPaint = new Paint(); final SimpleDateFormat amorpm = new SimpleDateFormat("a", Locale.US); if (amorpm.format(new Date()).equals("AM")) { ampmPaint.setColor(Co
Paint ampmPaint = new Paint();
final SimpleDateFormat amorpm = new SimpleDateFormat("a", Locale.US);
if (amorpm.format(new Date()).equals("AM")) {
ampmPaint.setColor(Color.RED);
}
else {
ampmPaint.setColor(Color.GREEN);
}
ampmPaint.setTextSize(context.getResources().getDimension(R.dimen.ampm_size));
ampmPaint.setAntiAlias(true);
ampmPaint.setTextSize(context.getResources().getDimension(R.dimen.ampm_size));
ampmPaint.setAntiAlias(true);
...
public void setAmColor(int color) {
ampmPaint.setColor(color);
}
public void setPmColor(int color) {
ampmPaint.setColor(color);
}
SimpleWatchFaceService.java
@Override
public void onAmbientModeChanged(boolean inAmbientMode) {
super.onAmbientModeChanged(inAmbientMode);
watchFace.setAntiAlias(!inAmbientMode);
if(inAmbientMode) {
watchFace.setPmColor(Color.GRAY);
watchFace.setAmColor(Color.GRAY);
}
else {
watchFace.setPmColor(Color.GREEN);
watchFace.setAmColor(Color.RED);
}
invalidate();
}
到目前为止,颜色在交互模式下工作,但一旦它转到环境模式并返回到交互模式,颜色就不工作,我知道我的代码不工作。我想知道如何修复我的代码,使其在交互模式下返回颜色您可以使用它获取一周中的某一天(1=周一,7=周日)。还要注意
if (amorpm.equals("AM")){
将始终返回false
,因为您正在检查SimpleDataFormat
是否等于字符串。正确的用法是:
SimpleDateFormat amorpm = new SimpleDateFormat("a", Locale.US);
SimpleDateFormat dayofweek = new SimpleDateFormat("u", Locale.US);
if (amorpm.format(new Date()).equals("AM")) {
// AM
...
和(请注意,您现在可以在字符串上执行以下操作):
至于环境模式,要检测您将需要一个As。您忘了问一个问题。对于环境模式,我使用onAmbientModeChanged,并将其包含在if/else语句watchFace.setAmPmColor(Color.GRAY);。public void setAmPmColor(int-color){ampmPaint.setColor(color);}我对我的问题进行了编辑,以便更好地表达我想做的事情
switch (dayofweek.format(new Date()) {
case "1":
// Monday
...