7
XML     
<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton1"
        android:src="@drawable/image1"
        android:onClick="buttonClick"
    />

JAVA
--------------------
public void buttonClick(View v)
{
    Button aButton = (Button)v;
    aButton.setBackgroundResource(R.drawable.image2);
}

这是我到目前为止没有运气的尝试......

我希望能够单击按钮并将图像更改为 image2,还有其他图像我将根据其他变量将其更改为。我真的被卡住了。我会继续研究其他问题,如果我找到答案,我会在这里发布。

4

1 回答 1

19

您的 buttonClick() 需要修复:

public void buttonClick(View v) 
{
 ImageButton aButton = (ImageButton)v;
 aButton.setImageResource(R.drawable.image2); 
} 

View 是一个 ImageButton,而不是一个 Button。src 属性通过 setImageResource 更新,而不是 setBackgroundResource。

于 2012-08-03T20:39:10.623 回答