如何在点击时更改按钮的图像?

How to change image of a button on click?

我有一个位图数组 ArrayList<Bitmap> images = new ArrayList<>(3) 并且需要在每次点击时更改 ImageButton 的图像,首先 images(0) 然后,在我点击 image(1) 之后,最后 image(2).

为此,我使用 myImageButton.setImageBitmap(images.get(0)) 作为第一张图片,如何更改为下一张和第三张?

你应该使用

int currentPos = 0;
onclick(){
 currentPos = (currentPos+1)%(images.size()-1)
 myImageButton.setImageBitmap(images.get(currentPos))
}

您可以使用

int currentPos = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        myImageButton.setImageBitmap(images.get(0))
}


@Override
public void onClick(View v){
    currentPos++;
    if(currentPos == 2){
        // if the imagebutton has the bitmap from position 2, you can do what you want
        //example:
        currentPos = 0;// so you have the first Bitmap you had when the user didn't click on the ImageButton
    }
    myImageButton.setImageBitmap(images.get(currentPos));
}