简体   繁体   中英

How to implement three different events with single button click in android?

I want to handle three different events for a button like when the button is TOUCHED, PRESSED and RELEASED. How is this possible? Please help me in this.

Here is my code that I have developed so far for handling two event but I need three events.

button.setOnTouchListener(new OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {

        // Check if the button is TOUCH
        if (event.getAction() == MotionEvent.ACTION_DOWN)  
        {

        }


        // Check if the button is RELEASED
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        {   

        }


            return true;
        }
});

What you have developed so far seems fine and should work for handling the touch and release events just fine. The long presses are a bit more complicated but nothing to fancy. You should preferrably use a GestureDetector . An example is given in this answer . Alternatively you can consult the official documentation .

Update: You may find the official tutorial on gestures quite useful. I believe especially the public void onLongPress(MotionEvent event) method will suit your needs!

You will try this i hope it will work fine for your cases... You just use this Activity and use this method for your implementation all the best... You just use the below listener class for the implementation of long press only....

public class MainActivity extends Activity {

private Button button;  
int check=0;
private GestureDetector  detector;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button)findViewById(R.id.button1);
    detector = new GestureDetector(new GestureListener());
    check=0;
    button.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector.onTouchEvent(event);
            int action = event.getActionMasked();               
            switch (action) {
            case MotionEvent.ACTION_MOVE:
                System.out.println("Move");
                break;
            case MotionEvent.ACTION_DOWN:
                System.out.println("Down");
                break;

            case MotionEvent.ACTION_UP:
                System.out.println("Released");
                break;              
            default:
                break;
            }
            return false;
        }
    });
}
class GestureListener implements OnGestureListener
{

    @Override
    public boolean onDown(MotionEvent e) {
        System.out.println("Down List");
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        System.out.println("Fly List");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        System.out.println("Long press");

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {

        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

        System.out.println("Press List");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("Single Tap List");
        return false;
    }

}
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM