在动态的ImageButton更改图像 [英] Changing image dynamically in an ImageButton

查看:133
本文介绍了在动态的ImageButton更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
}

下面就是我没有运气到目前为止已经试过...

Here's what I've tried so far with no luck...

我希望能够点击按钮,改变形象IMAGE2,还有将是其他图像我就改成了基于其它变量。我只是很坚持。我会继续寻找其他的问题,如果我找到一个答案,我会张贴在这里。

I want to be able to click the button and change the image to image2, there's also going to be other images i'll change it to based off of other variables. I'm just really stuck.. I'll continue looking at other questions and if I find an answer I'll post it here.

推荐答案

您buttonClick()需要固定:

Your buttonClick() needs fixing:

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

在View是一个ImageButton的,而不是一个按钮。 src属性是通过setImageResource更新,而不是setBackgroundResource。

the View is an ImageButton, not a Button. The src attribute is updated via setImageResource, not setBackgroundResource.

这篇关于在动态的ImageButton更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆