Reset switch button does not work properly

The toggle button does not go OFF in the UI view, and I set onCheckedChangeListener . It stays on and turns gray. This happens on Simulator with API15, but does not appear in API19 on my real device. Is this a code or a simulator?
The last sr.setChecked(false) allows only the gray button, but does not disable it.
Minimal example for reproducing behavior: class var:

 Switch sr; Switch srs; 

onCreate includes:

  sr = (Switch) findViewById(R.id.switch_ros); srs = (Switch) findViewById(R.id.switch_ros_stream); sr.setOnCheckedChangeListener(this); srs.setOnCheckedChangeListener(this); 

onCheckedChanged includes:

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { buttonView.setChecked(false); } 

EDIT : Instead of Nexus_4_API_15, I tried Nexus_5_API_19 and it works fine. This seems to be an Android bug. Simulator Image

+5
source share
1 answer

Put these two at the top of your class

 private Switch sr; private Switch srs; 

Then in onCreate ()

 @Override protected void onCreate( Bundle savedInstanceCreate ) { [...]// other onCreate() stuff sr = (Switch) findViewById(R.id.switch_ros); srs = (Switch) findViewById(R.id.switch_ros_stream); sr.setOnCheckedChangeListener(this); srs.setOnCheckedChangeListener(this); } 

Then OnCheckedChangeListener ()

 @Override public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) { switch(buttonView.getId()) { case R.id.switch_ros: //boolean wifi_state = isConnected(isChecked); sr.setChecked(isChecked); break; case R.id.switch_ros_stream: [...]// other switch function srs.setChecked(isChecked); break; } } 

Edit 1 OnCheckedChangeListener cannot find the switch button that you are trying to turn on / off. This is because you are throwing more than one switch into a method. You will need to use the switch case or the else statement to determine which button you want to switch.

+2
source

Source: https://habr.com/ru/post/1238138/


All Articles