如何将字符串添加到字符串数组

时间:2017-04-12 15:18:32

标签: android arrays

我是Android工作室的新手,我遇到了这种问题。我想使用fragB中的复选框从fragAfragA添加文字。当我选择一个CheckBox时,会出现文本但是当我选择另一个CheckBox时,应该添加的文本不可见。只有当我取消选中其他CheckBox时才能看到它。

请帮帮我。

这是我的碎片:

public class fragA extends Fragment implements View.OnClickListener{
    CheckBox c1,c2,c3;
    communicator comm;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_a,container,false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        comm=(communicator)getActivity();
        c1=(CheckBox)getActivity().findViewById(R.id.c1);
        c1.setOnClickListener(this);
        c2=(CheckBox)getActivity().findViewById(R.id.c2);
        c2.setOnClickListener(this);
        c3=(CheckBox)getActivity().findViewById(R.id.c3);
        c3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        String final_text=" ";
        ArrayList<String> selection=new ArrayList<String>();
        if(c1.isChecked()){
            selection.add("O");
            for(String Selections:selection){
                final_text=final_text+Selections+"\n";
            }
        }
        else if(c2.isChecked()) {
            selection.add("My");
            for(String Selections:selection){
                final_text=final_text+Selections+"\n";
            }
        }
        else if(c3.isChecked()) {
            selection.add("God");
            for(String Selections:selection){
                final_text=final_text+Selections+"\n";
            }
        }
            comm.respond(""+final_text);
    }
}

1 个答案:

答案 0 :(得分:0)

我在这里解决了自己的问题。我没有使用if eslse语句,而是使用了case。 现在,当我选中所有复选框时,所有文本都将显示,当我取消选中一个时,该复选框的文本将被删除。 我使用以下代码fragA

public class fragA extends Fragment implements View.OnClickListener{
    CheckBox c1,c2,c3;
    communicator comm;
    ArrayList<String> selection=new ArrayList<String>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_a,container,false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        comm=(communicator)getActivity();
        c1=(CheckBox)getActivity().findViewById(R.id.c1);
        c1.setOnClickListener(this);
        c2=(CheckBox)getActivity().findViewById(R.id.c2);
        c2.setOnClickListener(this);
        c3=(CheckBox)getActivity().findViewById(R.id.c3);
        c3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        boolean checked = ((CheckBox) v).isChecked();
        String final_work="";
        switch (v.getId()){
            case R.id.c1:
                if(checked) {
                    selection.add("O");
                    for (String Selections : selection) {
                        final_work = final_work + Selections + "\n";
                    }comm.respond(""+final_work);
                }

                else
                    {
                        selection.remove("O");
                        for (String Selections : selection) {
                            final_work = final_work + Selections + "\n";
                        }comm.respond(""+final_work);
                    }
                break;
            case R.id.c2:
                if(checked)
                {selection.add("My");
                    for (String Selections : selection) {
                            final_work = final_work + Selections + "\n";
                        }comm.respond(""+final_work);
                    }
                else
                {selection.remove("My");
                    for (String Selections : selection) {
                            final_work = final_work + Selections + "\n";
                        }comm.respond(""+final_work);
                    }
                break;
            case R.id.c3:
                if(checked)
                {selection.add("God");
                    for (String Selections : selection) {
                            final_work = final_work + Selections + "\n";
                        }comm.respond(""+final_work);
                    }
                else
                {selection.remove("God");
                    for (String Selections : selection) {
                            final_work = final_work + Selections + "\n";
                        }comm.respond(""+final_work);}
                break;
        }
    }
}