Switch button - disable swipe function

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

        
    
            
    
        
    

提交回复
热议问题