如何将两个不同片段中的整数添加到一个片段中

时间:2017-04-15 10:39:54

标签: android android-fragments android-studio-2.3

我刚刚接触到android studio,我遇到了这个问题。在我的Tab1(第一个片段)中,我有复选框,当你选择时,它会为Total(我发送所有值的片段)添加一个值,它可以正常工作。

enter image description here

然后当我在Tab2(第二个片段)中选择一个复选框时,该值将不会添加。简而言之,每个片段都有其总值。

enter image description here

现在我该如何添加它?

继承人我的Tab1:

public class Tab1 extends android.support.v7.app.AppCompatDialogFragment implements View.OnClickListener {
    CheckBox mc1,mc2,mc3,mc4,mc5,mc6,mc7,mc8,mc9,mc10,mc11,mc12;
    communicator comm;
    int total;

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

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        comm=(communicator)getActivity();
        mc1=(CheckBox)getActivity().findViewById(R.id.mc1);
        mc1.setOnClickListener(this);
        mc2=(CheckBox)getActivity().findViewById(R.id.mc2);
        mc2.setOnClickListener(this);
        mc3=(CheckBox)getActivity().findViewById(R.id.mc3);
        mc3.setOnClickListener(this);
        mc4=(CheckBox)getActivity().findViewById(R.id.mc4);
        mc4.setOnClickListener(this);
        mc5=(CheckBox)getActivity().findViewById(R.id.mc5);
        mc5.setOnClickListener(this);
        mc6=(CheckBox)getActivity().findViewById(R.id.mc6);
        mc6.setOnClickListener(this);
        mc7=(CheckBox)getActivity().findViewById(R.id.mc7);
        mc7.setOnClickListener(this);
        mc8=(CheckBox)getActivity().findViewById(R.id.mc8);
        mc8.setOnClickListener(this);
        mc9=(CheckBox)getActivity().findViewById(R.id.mc9);
        mc9.setOnClickListener(this);
        mc10=(CheckBox)getActivity().findViewById(R.id.mc10);
        mc10.setOnClickListener(this);
        mc11=(CheckBox)getActivity().findViewById(R.id.mc11);
        mc11.setOnClickListener(this);
        mc12=(CheckBox)getActivity().findViewById(R.id.mc12);
        mc12.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        boolean checked = ((CheckBox) v).isChecked();
        switch (v.getId()){
            case R.id.mc1:
                if(checked){
                    total=total+400;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-400;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc2:
                if(checked){
                    total=total+250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc3:
                if(checked){
                    total=total+350;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-350;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc4:
                if(checked){
                    total=total+400;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-400;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc5:
                if(checked){
                    total=total+250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc6:
                if(checked){
                    total=total+250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc7:
                if(checked){
                    total=total+250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-250;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc8:
                if(checked){
                    total=total+200;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-200;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc9:
                if(checked){
                    total=total+150;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-150;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc10:
                if(checked){
                    total=total+150;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-150;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc11:
                if(checked){
                    total=total+450;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-450;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
            case R.id.mc12:
                if(checked){
                    total=total+550;
                    comm.respond("Total:\n"+total+" Php.");
                }
                else{
                    total=total-550;
                    comm.respond("Total:\n"+total+" Php.");
                }
                break;
        }
    }
}

继承人我的Tab2:

public class Tab2 extends android.support.v7.app.AppCompatDialogFragment implements View.OnClickListener{
    CheckBox aoc1,aoc2,aoc3,aoc4;
    communicator comm;
    int total;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab2,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        comm=(communicator)getActivity();
        aoc1=(CheckBox)getActivity().findViewById(R.id.aoc1);
        aoc1.setOnClickListener(this);
        aoc2=(CheckBox)getActivity().findViewById(R.id.aoc2);
        aoc2.setOnClickListener(this);
        aoc3=(CheckBox)getActivity().findViewById(R.id.aoc3);
        aoc3.setOnClickListener(this);
        aoc4=(CheckBox)getActivity().findViewById(R.id.aoc4);
        aoc4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        boolean checked = ((CheckBox) v).isChecked();
        switch (v.getId()) {
            case R.id.aoc1:
                if (checked) {
                    total = total + 50;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 50;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;
            case R.id.aoc2:
                if (checked) {
                    total = total + 100;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 100;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;
            case R.id.aoc3:
                if (checked) {
                    total = total + 100;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 100;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;
            case R.id.aoc4:
                if (checked) {
                    total = total + 150;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 150;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;
        }
    }
}

这是我的总计:

public class Total extends Fragment {
    TextView ttl;

    public Total() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.total, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ttl= (TextView) getActivity().findViewById(R.id.total);
    }

}

这是我的respond中的MainActivity

@Override
    public void respond(String data) {
        ttl.setText(data);
    }

注意:我的Total Fragment在我的activity_main中,这就是为什么它只是浮动所以当我改变标签时它不会移动。

1 个答案:

答案 0 :(得分:0)

那是因为你在两个标签中分别创建了int total ..你应该保持它的共性...... 做这样的事情..

public Class CommonTotal{

private int total = 0;

   public int getTotal() {
      return total;
   } 
   public void setTotal(int total) {
      this.total = this.total + total ;
   }
}

创建CommonTotal的对象

CommonTotal commonTotal = new CommonTotal();

并在两个标签中引用它,而不是int total 而不是像

那样
case R.id.aoc1:
                if (checked) {
                    total = total + 50;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 50;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;

做这样的事情

case R.id.aoc1:
                if (checked) {
                    commonTotal.setTotal(50);//
                    //total = total + 50;
                    comm.respond("Total:\n" + total + " Php.");
                } else {
                    total = total - 50;
                    comm.respond("Total:\n" + total + " Php.");
                }
                break;

使用MVC模式...来获取错字。尝试从中获得一些意义。 希望我给你一些想法..如果这有助于标记我:) 要么 只需在MainActivity中创建两个方法,然后创建public int total

public void commonTotal(int t){
 total = total +t;
}

public void whatsthetotal(){
return total;
}

并调用此方法