活动中两个Frangments之间的计算

时间:2017-11-16 08:42:22

标签: android android-fragments

我想分别计算每个项目以及一起计算。还想打印数量。现在我只能打印总金额。但我必须在每个项目中插入值。如果我留空1,应用程序崩溃。

图片1是显示计算总金额的FrangmentActivity

image 1

当我留下1个空白输入并尝试计算

时,图像2正在崩溃

image 2

TopSectionFragment

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class TopSectionFragment extends Fragment {

private static EditText editText2, editText3, editText4;

TopSectionListener activityCommander;

public interface TopSectionListener {
    public void createQuantity(String result);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCommander = (TopSectionListener) activity;
    }catch (ClassCastException e){
        throw new ClassCastException(activity.toString());
    }
}

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


    editText2 = (EditText) view.findViewById(R.id.etQSmall);
    editText3 = (EditText) view.findViewById(R.id.etQMedium);
    editText4 = (EditText) view.findViewById(R.id.etQLarge);
    final Button button = (Button) view.findViewById(R.id.btnCal);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonClicked(v);
        }
    });

    return view;
}
public void buttonClicked(View view){

    int smallPizza = Integer.parseInt(editText2.getText().toString());
    int mediumPizza = Integer.parseInt(editText3.getText().toString());
    int largePizza = Integer.parseInt(editText4.getText().toString());

    //Do Calculation Small = 15, Medium = 20, Large = 25

    int total = (smallPizza * 15) + (mediumPizza * 20) + (largePizza * 
    25);

    String result = "Total RM "+total;
    activityCommander.createQuantity(result);
   }
}

BottomSectionFragment

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BottomSectionFragment extends Fragment{

private static TextView textAmount;
private static TextView textQuantity;

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

    textAmount = (TextView) view.findViewById(R.id.tvAmount);
    textQuantity = (TextView) view.findViewById(R.id.tvQuantity);

    return view;
}
public void setText(String amount){
    textAmount.setText(amount);
 }
}

FragmentActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FragmentActivity extends AppCompatActivity implements 
TopSectionFragment.TopSectionListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);
}

@Override
public void createQuantity(String amount) {
    BottomSectionFragment bottomFragment = (BottomSectionFragment) 
getSupportFragmentManager().findFragmentById(R.id.fragment2);
    bottomFragment.setText(amount);
  }
}

1 个答案:

答案 0 :(得分:0)

textbox内转换

之前,检查onClick() 为空是否
int smallPizza, mediumPizza ,largePizza;
if(!editText1.getText().toString().trim().isEmpty()){
   smallPizza = Integer.parseInt(editText1.getText().toString());
}else{
   smallPizza = 0;
}
if(!editText2.getText().toString().trim().isEmpty()){
   mediumPizza = Integer.parseInt(editText2.getText().toString());
}else{
   mediumPizza = 0;
}
if(!editText3.getText().toString().trim().isEmpty()){
   largePizza = Integer.parseInt(editText3.getText().toString());
}else{
   largePizza = 0;
}

int total = (smallPizza * 15) + (mediumPizza * 20) + (largePizza * 25);