How can I change the images on an ImageButton in Android when using a OnTouchListener?

前端 未结 2 1546
南方客
南方客 2021-02-19 21:29

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

ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1);
SoundBu         


        
相关标签:
2条回答
  • 2021-02-19 21:52

    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.

    0 讨论(0)
  • 2021-02-19 22:03
    SoundButton1.setOnTouchListener(new OnTouchListener() {
    
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                mSoundManager.playSound(1);
                btnmode.setImageResource(R.drawable.modeitempressed);
    
            }
             elseif (event.getAction() == MotionEvent.ACTION_UP ) {
                btnmode.setImageResource(R.drawable.modeitemnormal);
    
            }
    
            return false;
        }
    });
    
    0 讨论(0)
提交回复
热议问题