Android How do I correctly get the value from a Switch?

前端 未结 5 1297
醉酒成梦
醉酒成梦 2020-12-07 22:02

I\'m creating a Android application which uses a Switch.
I\'m trying to listen for changes and get the value when changed.
I have two questions when usi

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 22:56

    Switch switch = (Switch) findViewById(R.id.Switch2);
    
    switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            ...switch on..
                        } else {
                           ...switch off..
                        }
                    }
                });
    

    i hope this will solve your problem

提交回复
热议问题