我与图像资源R.drawable.img1
一个ImageButton
,我要像改变从R.drawable.img1
到R.drawable.img2
或R.drawable.img2
到R.drawable.img1
每次我点击ImageButton
。ImageButton第一次点击/第二次点击?
我是想通过这种方式,但它不工作:
Integer img1 = (Integer) gridlayout.getChildAt(index).getTag(R.drawable.img1);
Integer img2 = (Integer) gridlayout.getChildAt(index).getTag(R.drawable.img2);
if(img1 == R.drawable.img1){
//code...
}else if(img2 == R.drawable.img2){
//code...
}
我设置ImageButton
的动态与此代码:
ImageButton imgBtn1 = new ImageButton(this);
imgBtn1.setId(id);
imgBtn1.setImageResource(R.drawable.img1);
imgBtn1.setTag(R.drawable.ing1);
imgBtn1.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgBtn1.setPadding(5,5,5,5);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(-7,-7,-7,-7);
params.width = fieldSize;
params.height = fieldSize;
gridlayout.addView(imgBtn1, index, params);
谢谢!
是不是你维护一个布尔值false = image1;真正的image2。每次更改onClick按钮时都会触发。 –
我正在使用ImageButtons网格,所以这将会非常复杂。 –