如何切换ImageButton的图片src

How to toggle picture src of ImageButton

我有一个图像按钮并希望当用户单击它时 - 它会将其 src 更改为不同的可绘制对象 - 但我希望我在 xml 中定义的背景颜色保持不变。这是我到目前为止所做的代码,但没有用,因为我正在更改背景而不是源代码,但我将使用的是一般概念:

                public void onClick(View view) {
                if (bgenabled == true) {
                    holder.ib.setBackground(res.getDrawable(R.drawable.location_deactive));
                    bgenabled = false;
                } else { holder.ib.setBackground(res.getDrawable(R.drawable.location_active));
                         bgenabled = false;}

只需调用setImageDrawable即可替换您当前使用的图片 用你的 ImageButton.

ImageButton button;
...
button.setImageDrawable(getResources().getDrawable(R.drawable.new_image));