Android Studio:为 onClick imageButton 遍历 drawable 中的图像
Android Studio: Iterate through images in drawable for onClick imageButton
我目前设置了 8 个 imageButton,id 为 imageButton1 到 imageButton8。
当我单击特定按钮(例如 imageButton2)时,我想遍历名为 image1....image10 的可绘制文件夹中的图像,并为每次单击将图像设置为 imageButton。
我已经查看了各种 for 循环来解决这个问题,但它们无济于事。这是 for 循环尝试的片段:
26/4/16 更新
已解决
//UPDATED 25/4/16
ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
ImageButton[] btns;
int[] drawables = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8};
@Override
protected void onCreate(Bundle savedInstanceState) {
btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn2 = (ImageButton) findViewById(R.id.imageButton2);
btn3 = (ImageButton) findViewById(R.id.imageButton3);
btn4 = (ImageButton) findViewById(R.id.imageButton4);
btn5 = (ImageButton) findViewById(R.id.imageButton5);
btn6 = (ImageButton) findViewById(R.id.imageButton6);
btn7 = (ImageButton) findViewById(R.id.imageButton7);
btn8 = (ImageButton) findViewById(R.id.imageButton8);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
}
btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
}
int counter = 0;
@Override
public void onClick(View v)
{
counter ++;
for (int i = 0; i < btns.length; i++)
{
if (v.getId() == btns[i].getId())
{
if(counter ==8)
{
counter =0;
}
((ImageButton)v).setImageResource(drawables[counter]);
}
}
}
}
26/4/16 更新
已解决
另一个尝试是创建一个计数器来存储点击次数,并设置 if 语句根据每次点击更改图像。
这里的问题是考虑到我有多个按钮,它会创建大量代码。
最重要的是,当我 运行 在两个不同的按钮上执行此操作时,一旦我点击 imageButton1 几次,然后点击 imageButton2 几次,imageButton1 不再响应。
int counter =0;
@Override
public void onClick(View v)
{
counter++;
switch(v.getId())
{
case R.id.imageButton1:
if(counter == 1){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image1);
break;
}else if (counter == 2){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image2);
break;
}else if (counter == 3){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image3);
break;
}else if (counter == 4){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image4);
break;
}else if (counter == 5){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image5);
break;
}else if (counter == 6){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image6);
break;
}else if (counter == 7){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image7);
break;
}else if (counter == 8){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image8);
break;
}else if (counter == 9){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image9);
break;
}else if (counter == 10) {
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image10);
counter = 0;
break;
}
break;
我将不胜感激任何指导,同时,如果我解决了这个问题,我将重新发布一个编辑。干杯
试试这个
int counter = 1;
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.imageButton1:
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
if(counter == 1){
btn1.setImageResource(R.drawable.image1);
}else if (counter == 2){
btn1.setImageResource(R.drawable.image2);
}else if (counter == 3){
btn1.setImageResource(R.drawable.image3);
}else if (counter == 4){
btn1.setImageResource(R.drawable.image4);
}else if (counter == 5){
btn1.setImageResource(R.drawable.image5);
}else if (counter == 6){
btn1.setImageResource(R.drawable.image6);
}else if (counter == 7){
btn1.setImageResource(R.drawable.image7);
}else if (counter == 8){
btn1.setImageResource(R.drawable.image8);
}else if (counter == 9){
btn1.setImageResource(R.drawable.image9);
}else if (counter == 10) {
btn1.setImageResource(R.drawable.image10);
}
counter = (counter % 10) + 1 ;
break;
}
只是一个建议。
也许您可以将按钮和图像资源声明为数组,然后循环遍历它们。
例如:
public class Sample extends Activity {
ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
ImageButton[] btns;
int[] drawables = new int[]{R.drawable.imageButton1,R.drawable.imageButton2,R.drawable.imageButton3,R.drawable.imageButton4,R.drawable.imageButton5,R.drawable.imageButton6,R.drawable.imageButton7,R.drawable.imageButton8};
int[] origDrawables = new int[]{R.drawable.imageOrigButton1,R.drawable.imageOrigButton2,R.drawable.imageOrigButton3,R.drawable.imageOrigButton4,R.drawable.imageOrigButton5,R.drawable.imageOrigButton6,R.drawable.imageOrigButton7,R.drawable.imageOrigButton8};
@Override
public void onCreate(Bundle saveInstanceState) {
btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn2 = (ImageButton) findViewById(R.id.imageButton2);
btn3 = (ImageButton) findViewById(R.id.imageButton3);
btn4 = (ImageButton) findViewById(R.id.imageButton4);
btn5 = (ImageButton) findViewById(R.id.imageButton5);
btn6 = (ImageButton) findViewById(R.id.imageButton6);
btn7 = (ImageButton) findViewById(R.id.imageButton7);
btn8 = (ImageButton) findViewById(R.id.imageButton8);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
}
@Override
public void onClick(View v) {
for (int i = 0; i < btns.length; i++) {
if (v.getId == btns[i].getId) {
((ImageButton)v).setImageResource(drawables[i]);
} else {
((ImageButton)v).setImageResource(origDrawables[i]);
}
}
}
}
注意:请原谅我使用记事本执行此操作时出现的任何 code/syntax 错误。
我目前设置了 8 个 imageButton,id 为 imageButton1 到 imageButton8。
当我单击特定按钮(例如 imageButton2)时,我想遍历名为 image1....image10 的可绘制文件夹中的图像,并为每次单击将图像设置为 imageButton。
我已经查看了各种 for 循环来解决这个问题,但它们无济于事。这是 for 循环尝试的片段:
26/4/16 更新
已解决
//UPDATED 25/4/16
ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
ImageButton[] btns;
int[] drawables = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8};
@Override
protected void onCreate(Bundle savedInstanceState) {
btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn2 = (ImageButton) findViewById(R.id.imageButton2);
btn3 = (ImageButton) findViewById(R.id.imageButton3);
btn4 = (ImageButton) findViewById(R.id.imageButton4);
btn5 = (ImageButton) findViewById(R.id.imageButton5);
btn6 = (ImageButton) findViewById(R.id.imageButton6);
btn7 = (ImageButton) findViewById(R.id.imageButton7);
btn8 = (ImageButton) findViewById(R.id.imageButton8);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
}
btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
}
int counter = 0;
@Override
public void onClick(View v)
{
counter ++;
for (int i = 0; i < btns.length; i++)
{
if (v.getId() == btns[i].getId())
{
if(counter ==8)
{
counter =0;
}
((ImageButton)v).setImageResource(drawables[counter]);
}
}
}
}
26/4/16 更新 已解决
另一个尝试是创建一个计数器来存储点击次数,并设置 if 语句根据每次点击更改图像。
这里的问题是考虑到我有多个按钮,它会创建大量代码。
最重要的是,当我 运行 在两个不同的按钮上执行此操作时,一旦我点击 imageButton1 几次,然后点击 imageButton2 几次,imageButton1 不再响应。
int counter =0;
@Override
public void onClick(View v)
{
counter++;
switch(v.getId())
{
case R.id.imageButton1:
if(counter == 1){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image1);
break;
}else if (counter == 2){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image2);
break;
}else if (counter == 3){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image3);
break;
}else if (counter == 4){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image4);
break;
}else if (counter == 5){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image5);
break;
}else if (counter == 6){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image6);
break;
}else if (counter == 7){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image7);
break;
}else if (counter == 8){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image8);
break;
}else if (counter == 9){
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image9);
break;
}else if (counter == 10) {
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn1.setImageResource(R.drawable.image10);
counter = 0;
break;
}
break;
我将不胜感激任何指导,同时,如果我解决了这个问题,我将重新发布一个编辑。干杯
试试这个
int counter = 1;
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.imageButton1:
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
if(counter == 1){
btn1.setImageResource(R.drawable.image1);
}else if (counter == 2){
btn1.setImageResource(R.drawable.image2);
}else if (counter == 3){
btn1.setImageResource(R.drawable.image3);
}else if (counter == 4){
btn1.setImageResource(R.drawable.image4);
}else if (counter == 5){
btn1.setImageResource(R.drawable.image5);
}else if (counter == 6){
btn1.setImageResource(R.drawable.image6);
}else if (counter == 7){
btn1.setImageResource(R.drawable.image7);
}else if (counter == 8){
btn1.setImageResource(R.drawable.image8);
}else if (counter == 9){
btn1.setImageResource(R.drawable.image9);
}else if (counter == 10) {
btn1.setImageResource(R.drawable.image10);
}
counter = (counter % 10) + 1 ;
break;
}
只是一个建议。
也许您可以将按钮和图像资源声明为数组,然后循环遍历它们。
例如:
public class Sample extends Activity {
ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
ImageButton[] btns;
int[] drawables = new int[]{R.drawable.imageButton1,R.drawable.imageButton2,R.drawable.imageButton3,R.drawable.imageButton4,R.drawable.imageButton5,R.drawable.imageButton6,R.drawable.imageButton7,R.drawable.imageButton8};
int[] origDrawables = new int[]{R.drawable.imageOrigButton1,R.drawable.imageOrigButton2,R.drawable.imageOrigButton3,R.drawable.imageOrigButton4,R.drawable.imageOrigButton5,R.drawable.imageOrigButton6,R.drawable.imageOrigButton7,R.drawable.imageOrigButton8};
@Override
public void onCreate(Bundle saveInstanceState) {
btn1 = (ImageButton) findViewById(R.id.imageButton1);
btn2 = (ImageButton) findViewById(R.id.imageButton2);
btn3 = (ImageButton) findViewById(R.id.imageButton3);
btn4 = (ImageButton) findViewById(R.id.imageButton4);
btn5 = (ImageButton) findViewById(R.id.imageButton5);
btn6 = (ImageButton) findViewById(R.id.imageButton6);
btn7 = (ImageButton) findViewById(R.id.imageButton7);
btn8 = (ImageButton) findViewById(R.id.imageButton8);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
}
@Override
public void onClick(View v) {
for (int i = 0; i < btns.length; i++) {
if (v.getId == btns[i].getId) {
((ImageButton)v).setImageResource(drawables[i]);
} else {
((ImageButton)v).setImageResource(origDrawables[i]);
}
}
}
}
注意:请原谅我使用记事本执行此操作时出现的任何 code/syntax 错误。