2010-04-11 13 views
11

Ho il seguente codice che crea un ImageButton e riproduce un suono quando si fa clic:Come posso modificare le immagini su un ImageButton in Android quando si utilizza un OnTouchListener?

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

Il problema è che voglio l'immagine sul ImageButton a cambiare quando si preme. OnTouchListener sembra sovrascrivere il tocco e non consentire la modifica delle immagini. Non appena rimuovo OnTouchListener, ImageButton si scambia su un'immagine diversa quando viene premuto. Qualche idea su come posso cambiare le immagini su ImageButton mentre utilizzo ancora OnTouchListener? Grazie mille!

risposta

4

Penso che la soluzione sia semplice: rimuovere lo return true dal file ontouchlistener. Poiché ciò blocca tutte le altre operazioni che rispondono al tocco e all'input. Fallo anche a return false.

In questo modo è possibile che altre azioni rispondano anche al tocco.

+0

Grazie! Ha funzionato. – codeman

32
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; 
    } 
}); 
Problemi correlati