要检查是否在另一个片段的复选框被选中 [英] want to check if a CheckBox in another fragment is Checked

查看:114
本文介绍了要检查是否在另一个片段的复选框被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个片段,我想从片段1至检查,看看是否在CheckBox1片段2被选中。

I have 2 fragments and I would like to check from Fragment 1 to see if CheckBox1 in fragment 2 is checked.

修改

我做以下几点:
在主要活动:

I am doing the following: In the main Activity:

@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

在Frgment是复选框可用:

In the Frgment were the checkbox is available:

@TargetApi(11)
public class tabquests extends Fragment{ 

    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) {
                  if(lc.isChecked())
                      lf_ch=true;//Wrong
                  else
                      lf_ch=false;//Wrong
              }
          });

在这里我想读lf_ch片段;

In the fragment where I want to read lf_ch;

public void onClick(View v) {
                if(lf_ch==false)//Wrong
                {

这是高级别什么,我想做的事情。所以我想一个方法来访问活动lf_ch集或直接访问复选框,在第2个片段。

This is in high level what I want to do. So I would like a way to access lf_ch set on Activity or to directly access the checkbox in the 2nd fragment.

推荐答案

您不应该直接从另一个访问中一个片段输入的状态。

You should not be directly accessing the state of an input in one fragment from another.

这是更好地创建活动水平的变量保存由认证机构指定的布尔值。

It is better to create a activity level variable to save the boolean value indicated by the cb.

public boolean lf_ch = false; // default checked state.

初​​始化您的CB的选中状态,以您的变量的值。

Initialize the checked state of your cb to the value of your variable.

lc.setChecked(context.getActivity().lf_ch);

然后,修改变量的值时,检查状态的变化。

Then change the value of your variable when the checked state changes.

lc.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                context.getActivity().lf_ch = isChecked;

            }});

然后在frag1您可以测试If_ch的价值;

Then in frag1 you can test the value of If_ch;

if(context.getActivity().If_ch){
    // Do something
}

-----编辑-------

----- EDIT -------

@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;
              }
          });

您将需要在这个类来定义格林尼治标准​​时间为好。

You will need to define gmt in this class as well.

public void onClick(View v) {
                if(!gmt.lf_ch)
                {

这篇关于要检查是否在另一个片段的复选框被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆