在Android的ImageView的切换按钮 [英] ImageView Button Toggle in Android
本文介绍了在Android的ImageView的切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使一个ImageView的按钮切换,当我点击它。我有以下code:
I'm trying to make an ImageView button toggle when I click on it. I've got the below code:
ImageView button01 = (ImageView) findViewById(R.id.button01);
button01.setOnClickListener(new OnClickListener() {
int button01pos = 0;
public void onClick(View v) {
if (button01pos == 0) {
button01.setImageResource(R.drawable.image01);
button01pos = 1;
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.image02);
button01pos = 0;
}
}
});
但由于某些原因button01是强调了红色Eclipse和它给人的错误:
But for some reason button01 is underlined in red in Eclipse and it gives the error:
不能引用非最终变量button01一个内部类中
在不同的方法定义
Cannot refer to a non-final variable button01 inside an inner class defined in a different method
有谁知道它为什么这样做,以及如何解决它?
Does anyone know why it's doing this and how to fix it?
感谢
推荐答案
下面是工作code:
final ImageView button01 = (ImageView) findViewById(R.id.button01);
button01.setOnClickListener(new OnClickListener() {
int button01pos = 0;
public void onClick(View v) {
if (button01pos == 0) {
button01.setImageResource(R.drawable.image01);
button01pos = 1;
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.image02);
button01pos = 0;
}
}
});
这篇关于在Android的ImageView的切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文