java - 两个不同的 Activity 中有两个不同的 SwitchCompat

标签 java android android-layout android-activity

我在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/

相关文章:

android - 如何将其添加到 fragment 中

java - 当我在布局之间滑动时如何打开音频文件?

android - 膨胀类 com.andexert.expandablelayout.library.ExpandableLayout 时出错

java - Azure 服务总线中的 ServiceBusSenderClient 和 ServiceBusSenderAsyncClient 有什么区别?

java.lang.NoClassDefFoundError : org/apache/http/client/HttpClient

java - Frustum near plane OpenGL 使对象看起来更近,而不仅仅是剪裁

android - 为什么 Android 测试运行程序报告 "Empty test suite"?

java - 永远隐藏导航栏

java - 通过java驱动程序连接到mongo shard 3.2.2

java - 单线程java程序加载多核