zcweng / SwitchButton

SwitchButton.An beautiful+lightweight+custom-style-easy switch widget for Android,minSdkVersion >= 11
https://github.com/zcweng/SwitchButton
MIT License
2.69k stars 415 forks source link

switch button not have On Click Listener #30

Open AbdElRahmanJamal opened 6 years ago

AbdElRahmanJamal commented 6 years ago

@Override public final void setOnClickListener(OnClickListener l) {}

can you please add its implementation

JobGetabu commented 4 years ago
switch.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                // Do what you want
                true
            } else false
        }
khaled-qawafel commented 2 years ago

If you want to use "switch.isChecked" boolean in a listener you can use @JobGetabu and wrap the switch in a ViewGroup so that you can use "switch.isChecked" in it's listener like this

switch.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                switchWrapper.performClick()
            true
        }

switchWrapper.setOnClickListener {
val newSwitchStatus = status.isChecked().not()
if (newSwitchStatus)
// Do what you want
status.isChecked() = newSwitchStatus
}