我在2个不同的 Activity 中有2个不同的SwithCompat,我想要实现的是,如果我单击 Activity A中的SwitchCompat, Activity B中的SwitchCompat也会被单击。反之亦然。
我可以用这段代码实现这个吗:
aSwitch = findViewById(R.id.switchs);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//do something
}
}
});
最佳答案
首先,当您通过 bundle 打开第二个 Activity 时,通过 bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
并在第二个 Activity 中获取 bundle
boolean value = getIntent().getExtras().getBoolean(key);
根据 boolean 值可以更改开关的开启和关闭
关于java - 两个不同的 Activity 中有两个不同的 SwitchCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53645668/