随一天中的时间更改 imageView
Change imageView with time of day
我正在尝试根据一天中的时间更新 imageView,但由于某种原因它无法正常工作并且没有出现任何编译错误。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Calendar cal = Calendar.getInstance();
int timeOfDay = cal.get(Calendar.HOUR_OF_DAY);
if (timeOfDay >=20 && timeOfDay <=4) {
imageView.setImageResource(R.drawable.night);
}else if (timeOfDay >= 5 && timeOfDay <= 8) {
imageView.setImageResource(R.drawable.sunset);
} else if (timeOfDay >= 9 && timeOfDay <= 16) {
imageView.setImageResource(R.drawable.morning);
}
else if (timeOfDay >= 17 && timeOfDay <= 19) {
imageView.setImageResource(R.drawable.sunset);
}
}
Activity.xml代码:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="606dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
您的代码仅适用于 5 到 19,因为 (timeOfDay >=20 && timeOfDay <=4)
returns 始终为 false。
这个if (timeOfDay >=20 && timeOfDay <=4)
条件没有执行(false)。因为没有时间 >=20 和 <=4
根据你的情况,你还需要这两个条件
if (timeOfDay >=0 && timeOfDay <=4)
和
else if (timeOfDay >=20 && timeOfDay <=24)
为它添加这两个和相关图像并删除不执行上述条件
你可以像下面这样简单地做
if (timeOfDay >= 5 && timeOfDay <= 8) {
imageView.setImageResource(R.drawable.sunset);
} else if (timeOfDay >= 9 && timeOfDay <= 16) {
imageView.setImageResource(R.drawable.morning);
}
else if (timeOfDay >= 17 && timeOfDay <= 19) {
imageView.setImageResource(R.drawable.sunset);
}
else{
//exectue this for 0-4 and 20-24
imageView.setImageResource(R.drawable.night);
}
(timeOfDay >=20 && timeOfDay <=4)
条件不可达。因为没有 >=20 和 <=4 的时间。在这种情况下,因为 timeOfDay 总是 > 0 所以将代码更改为
(timeOfDay >=20 || timeOfDay <=4)
并且不要忘记重新格式化您的代码。
我正在尝试根据一天中的时间更新 imageView,但由于某种原因它无法正常工作并且没有出现任何编译错误。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Calendar cal = Calendar.getInstance();
int timeOfDay = cal.get(Calendar.HOUR_OF_DAY);
if (timeOfDay >=20 && timeOfDay <=4) {
imageView.setImageResource(R.drawable.night);
}else if (timeOfDay >= 5 && timeOfDay <= 8) {
imageView.setImageResource(R.drawable.sunset);
} else if (timeOfDay >= 9 && timeOfDay <= 16) {
imageView.setImageResource(R.drawable.morning);
}
else if (timeOfDay >= 17 && timeOfDay <= 19) {
imageView.setImageResource(R.drawable.sunset);
}
}
Activity.xml代码:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="606dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
您的代码仅适用于 5 到 19,因为 (timeOfDay >=20 && timeOfDay <=4)
returns 始终为 false。
这个if (timeOfDay >=20 && timeOfDay <=4)
条件没有执行(false)。因为没有时间 >=20 和 <=4
根据你的情况,你还需要这两个条件
if (timeOfDay >=0 && timeOfDay <=4)
和
else if (timeOfDay >=20 && timeOfDay <=24)
为它添加这两个和相关图像并删除不执行上述条件
你可以像下面这样简单地做
if (timeOfDay >= 5 && timeOfDay <= 8) {
imageView.setImageResource(R.drawable.sunset);
} else if (timeOfDay >= 9 && timeOfDay <= 16) {
imageView.setImageResource(R.drawable.morning);
}
else if (timeOfDay >= 17 && timeOfDay <= 19) {
imageView.setImageResource(R.drawable.sunset);
}
else{
//exectue this for 0-4 and 20-24
imageView.setImageResource(R.drawable.night);
}
(timeOfDay >=20 && timeOfDay <=4)
条件不可达。因为没有 >=20 和 <=4 的时间。在这种情况下,因为 timeOfDay 总是 > 0 所以将代码更改为
(timeOfDay >=20 || timeOfDay <=4)
并且不要忘记重新格式化您的代码。