Triggering event when Button is pressed down in Android

后端 未结 3 1669
一向
一向 2020-11-30 06:51

I have the following code for Android which works fine to play a sound once a button is clicked:

Button SoundButton2 = (Button)findViewById(R.id.sound2);
            


        
相关标签:
3条回答
  • 2020-11-30 06:58

    Maybe using a OnTouchListener? I guess MotionEvent will have some methods for registering a touch on the object.

       button.setOnTouchListener(new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
         // TODO Auto-generated method stub
         return false;
        }
       }))
    
    0 讨论(0)
  • 2020-11-30 06:58

    You should do this: b is the button.

    b.setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                        mSoundManager.playSound(2);
                        return true;
                    }
    
                    return false;
                }
            });
    
    0 讨论(0)
  • 2020-11-30 07:16

    import android.view.MotionEvent;

    0 讨论(0)
提交回复
热议问题