Switch button - disable swipe function

后端 未结 6 574
名媛妹妹
名媛妹妹 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!

    0 讨论(0)
  • 2020-12-17 09:52

    To disable a Switch use following method

    switchBtn.setEnabled(false);
    

    To make switch not clickable use

    switchBtn.setClickable(false);
    

    where switchBtn is Switch object

    0 讨论(0)
  • 2020-12-17 09:56

    My solution is:

    switchView.setOnTouchListener(new View.OnTouchListener() { 
    @Override           
    public boolean onTouch(View view, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                                //Your code
                    }
                    return true;            
        }       
    });
    

    Or ButterKnife:

    @OnTouch(R.id.switchView)
    public boolean switchStatus(Switch switchView, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // your code
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-12-17 10:06

    You can custom a view that extends Switch then override its onTouchEvent()

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean handle = ev.getActionMasked() == MotionEvent.ACTION_MOVE;
        return handle ? handle : super.onTouchEvent(ev);
    }
    

    It should work.

    0 讨论(0)
  • 2020-12-17 10:08

    You can setClickable(false) to your Switch, then listen for the onClick() event within the Switch's parent and toggle it programmatically. The switch will still appear to be enabled but the swipe animation won't happen.

    ...

    [In onCreate()]

    Switch switchInternet = (Switch) findViewById(R.id.switch_internet);
    switchInternet.setClickable(false);
    

    ...

    [click listener]

    public void ParentLayoutClicked(View v){
        Switch switchInternet = (Switch) findViewById(R.id.switch_internet);
    
        if (switchInternet.isChecked()) {
            switchInternet.setChecked(false);           
        } else {
            switchInternet.setChecked(true);    
        }       
    }
    

    ...

    [layout.xml]

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:onClick="ParentLayoutClicked"
            android:focusable="false"
            android:orientation="horizontal" >
    
            <Switch
                android:layout_marginTop="5dip"
                android:layout_marginBottom="5dip"
                android:id="@+id/switch_internet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOff="NOT CONNECTED"
                android:textOn="CONNECTED"
                android:focusable="false"
                android:layout_alignParentRight="true"                                                
                android:text="@string/s_internet_status" />
    
        </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-17 10:09

    //This code works fine

    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { Switch my_switch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        my_switch = (Switch) findViewById(R.id.my_switch);
        my_switch.setOnCheckedChangeListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_switch:
                if(my_switch.isChecked())
                    my_switch.setChecked(true);                
                else           
                    my_switch.setChecked(true);
                break;
        }
    }
    

    }

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