[英]Switch Statement using a loop in Java
I have a switch statement to change image resourceId: 我有一个switch语句来更改图像resourceId:
int imageId = 0;
switch (i) {
case 0:
imageId = R.drawable.image0;
break;
case 1:
imageId = R.drawable.image1;
break;
case 2:
imageId = R.drawable.image2;
break;
case 3:
imageId = R.drawable.image3;
break;
case 4:
imageId = R.drawable.image4;
break;
case 5:
imageId = R.drawable.image5;
break;
case 6:
imageId = R.drawable.image6;
break;
case 7:
imageId = R.drawable.image7;
break;
}
But I have been trying to find out how to do this using a loop, since the case number and the number at the end of each image matches. 但是我一直在尝试使用循环来执行此操作,因为案例编号和每个图像末尾的编号都匹配。 Tried with a for loop but without success. 尝试了for循环,但没有成功。
Can anyone help? 有人可以帮忙吗?
Thx!! 谢谢!!
Why not give R.drawable an array of images and then you can do the below ? 为什么不给R.drawable一个图像数组 ,然后可以执行以下操作?
imageId = R.drawable.images[i];
Here's the Oracle tutorial . 这是Oracle教程 。 Alternatives include a java.util.List
of some particular implementation eg an ArrayList
备选方案包括某些特定实现的java.util.List
,例如ArrayList
hey why you not do like this, its simple and easy for beginner, like we make click on onclicklistener, 嘿,为什么您不喜欢这种方法,它对于初学者来说很简单,就像我们点击onclicklistener一样,
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.rlfeatured:
// perform any action
break;
case R.id.rlbrowse:
// perform any action
break;
case R.id.rlmyshows:
break;
case R.id.rlcategories:
break;
case R.id.rlfavourites:
break;
default:
break;
}
}
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.