您不应该直接从另一个片段访问一个片段中的输入状态。
最好创建一个活动级别变量来保存 cb 指示的布尔值。
public boolean lf_ch = false; // default checked state.
将 cb 的检查状态初始化为变量的值。
lc.setChecked(context.getActivity().lf_ch);
然后,当选中状态发生变化时,更改变量的值。
lc.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
context.getActivity().lf_ch = isChecked;
}});
然后在frag1中可以测试If_ch的值;
if(context.getActivity().If_ch){
// Do something
}
- - - 编辑 - - - -
@TargetApi(11)
public class gamesmodestab extends Activity{
public static Context appContext;
public boolean lf_ch=false;
public void onCreate(Bundle savedInstanceState){
appContext=this;
super.onCreate(savedInstanceState);
//Then I declare the fragments and add them
In the Frgment were the checkbox is available:
@TargetApi(11)
public class tabquests extends Fragment{
gamesmodestab gmt = (gamesmodestab) getActivity();
public CheckBox lc,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
{
View V=inflater.inflate(R.layout.tab_quests, container, false);
lc=(CheckBox)V.findViewById(R.id.CheckBox01);
lc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
gmt.If_ch = isChecked;
}
});
您还需要在此类中定义 gmt。
public void onClick(View v) {
if(!gmt.lf_ch)
{