选中复选框后的android显示片段

时间:2014-11-03 09:36:12

标签: android checkbox fragment

我很熟悉Android编程和我的简单程序,

  

我正在尝试在选中CheckBox时显示片段并删除   未选中CheckBox时的片段。

在我的代码中,当我选中CheckBox时,会显示片段,当我禁用时,CheckBox片段会被删除。然而,当我试图再次展示它时,我无法成功。

这是我关于复选框的代码的一部分

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(checkBox.isChecked())
            {
                checkBox.setText("It is on.");
                MyFragment fragment = (MyFragment)getFragmentManager().findFragmentById(R.id.container);
                if (fragment == null) 
                {
                    getFragmentManager().beginTransaction()
                            .add(R.id.container, new MyFragment()).
                            commit();
                }
            }

            else
            {
                checkBox.setText("It is off.");

                MyFragment fragment = (MyFragment)getFragmentManager().findFragmentById(R.id.container);
                if (fragment != null) 
                {
                    getFragmentManager().beginTransaction()
                        .remove(fragment)
                        .commit();
                    }
            }
        }
    });

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

尝试这样,

@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
              MyFragment fragment = (MyFragment)getFragmentManager().findFragmentById(R.id.container);
            if(checkBox.isChecked())
            {
                checkBox.setText("It is on.");

                    getFragmentManager().beginTransaction()
                            .add(R.id.container, fragment).
                            commit();

            }

            else
            {
                checkBox.setText("It is off.");             
                if (fragment != null) 
                {
                    getFragmentManager().beginTransaction()
                        .remove(fragment)
                        .commit();
                    }
            }
        }
    });

答案 1 :(得分:0)

你不可能添加它,因为你完全删除它,尝试去掉它。通过分离它,Fragment保持其状态。

试试这个:

getFragmentManager().beginTransaction()
                        .detach(fragment)
                        .commit();
                    }

删除意味着无法重新附加片段实例。您将不得不再次将其添加到片段事务中。

答案 2 :(得分:0)

尝试这样:

1)为片段

创建布局
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:background="#ff0000">
  </LinearLayout>

2)创建扩展Fragment

的片段类
 public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
           return inflater.inflate(R.layout.myfragment, container,false);
    }
 }

3)在activity_main.xml中放置一个id为

的空布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/frag">
    </LinearLayout>
</LinearLayout>

4)在MainActivity.java中,创建Fragment类的对象,然后使用FragmentManager添加和删除它

public class MainActivity extends Activity {

     CheckBox cbox;
     FragmentManager manager;
     MyFragment frag;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = (FragmentManager) getFragmentManager();
        frag = new MyFragment();

        cbox = (CheckBox) findViewById(R.id.checkBox1);
        cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true){
                    manager.beginTransaction().add(R.id.frag, frag).commit();
                }
                else{
                    manager.beginTransaction().remove(frag).commit();
                }
            }
        });
    }
}