使用OnTouchListener时,我怎样才能改变图像在Android的一个ImageButton的? [英] How can I change the images on an ImageButton in Android when using a OnTouchListener?

查看:116
本文介绍了使用OnTouchListener时,我怎样才能改变图像在Android的一个ImageButton的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,它生成一个ImageButton的和播放点击时声音:

I have the following code which creates an ImageButton and plays a sound when clicked:

ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1);
SoundButton1.setImageResource(R.drawable.my_button);

SoundButton1.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN ) {
            mSoundManager.playSound(1);
            return true;
        }

        return false;
    }
});

现在的问题是,我想在图像上的ImageButton时,preSS它改变。所述OnTouchListener似乎是覆盖触摸和不允许的图像发生变化。当我删除OnTouchListener时,ImageButton的掉期交易,以不​​同的图像时pressed。我如何能有图像的任何想法更改,同时仍然使用OnTouchListener的ImageButton的?非常感谢你!

The problem is that I want the image on the ImageButton to change when you press it. The OnTouchListener appears to be overriding the touch and not allowing the images to change. As soon as I remove the OnTouchListener, the ImageButton swaps to a different image when pressed. Any ideas on how I can have the images change on the ImageButton while still using the OnTouchListener? Thank you very much!

推荐答案

我认为,解决方法很简单:从ontouchlistener删除返回true 。由于阻止所有响应触摸输入进一步的操作。让它返回false 了。

I think the solution is simple: remove the return true from the ontouchlistener. Since that blocks all further operations that respond to touch and input. Make it return false too.

这种方法能够让其他行动,也向触摸做出反应。

This way it will allow other actions to also respond to the touch.

这篇关于使用OnTouchListener时,我怎样才能改变图像在Android的一个ImageButton的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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