Switch button - disable swipe function

后端 未结 6 583
名媛妹妹
名媛妹妹 2020-12-17 09:42

I have a switch button (actually is a custom one) and I want to disable the swipe functionality for some reason; I want the user to be able to click it only. Is

6条回答
  •  执念已碎
    2020-12-17 09:48

    A better way is to prevent the Switch class from receiving MotionEvent.ACTION_MOVE events.

    This can be done with:

    switchButton.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        return event.getActionMasked() == MotionEvent.ACTION_MOVE;
      }
    });
    

    Then you are free to set a click listener on the switch as appropriate.

    Check out the implementation of Switch to see how dragging works. It's pretty cool!

提交回复
热议问题