Android: on switch not called when toggle by js code

This commit is contained in:
王劲鹏 2021-03-08 11:16:12 +08:00 committed by osborn
parent 9c166da915
commit 2d5d24ac40

View File

@ -39,6 +39,8 @@ public class SwitchNode extends ViewNode<SwitchCompat> {
private int onTintColor = 0xff52d769; private int onTintColor = 0xff52d769;
private int thumbTintColor = Color.WHITE; private int thumbTintColor = Color.WHITE;
private boolean checkByCodeToggle = false;
public SwitchNode(DoricContext doricContext) { public SwitchNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@ -55,7 +57,9 @@ public class SwitchNode extends ViewNode<SwitchCompat> {
if (!prop.isBoolean()) { if (!prop.isBoolean()) {
return; return;
} }
checkByCodeToggle = true;
view.setChecked(prop.asBoolean().value()); view.setChecked(prop.asBoolean().value());
checkByCodeToggle = false;
break; break;
case "onSwitch": case "onSwitch":
if (!prop.isString()) { if (!prop.isString()) {
@ -65,6 +69,7 @@ public class SwitchNode extends ViewNode<SwitchCompat> {
view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkByCodeToggle) return;
callJSResponse(callbackId, isChecked); callJSResponse(callbackId, isChecked);
} }
}); });