Java 使用片段中的开关按钮更改文本
如何在其他片段中检查开关位置 第一个片段Java 使用片段中的开关按钮更改文本,java,android,Java,Android,如何在其他片段中检查开关位置 第一个片段 textView=view.findViewById(R.id.textview_first); // check second fragment switch position and change text 第二段 aSwitch=view.findViewById(R.id.switch1); aSwitch.setOnCheckedChangeListener(new CompoundButt
textView=view.findViewById(R.id.textview_first);
// check second fragment switch position and change text
第二段
aSwitch=view.findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//Checnge first fragment textview text to checked
}else {
//Checnge first fragment textview text to unchecked
}
}
});
您要做的是(片段之间的通信): 带字符串(选中/未选中) 我假设您没有使用ViewModels/LiveData,否则您也可以用这些解决问题 编辑: 刚刚看到您正在使用Java,您可以在这里找到Java示例和通用android指南:
为了使用java在两个片段之间进行通信,大多数情况下,我们采用了接口的概念。我们只需要使用一个接口,然后在fragments类上设置侦听器,然后在控制片段的主类上实现操作侦听器
您可以将其编辑为更高的代码吗。在侦听接口调用的片段中。只需将前面片段中的字符串输入到
.toUppercase()
中,就可以将其转换为大写。您可以将其编辑为大写代码吗