如何在选项卡式活动的片段中获取“计算按钮”

时间:2018-12-29 10:49:59

标签: java android

我正在创建一个应用程序,其中三个cell?.imageView?.image = UIImage(named: "Default") 和三个tabs为三个选项卡分别创建,三个选项卡代表bmi计算器,长度转换器和重量转换器。没有任何计算按钮可以在任何片段中使用

我尝试从fragments设置onclick,并且覆盖xml文件中的onclick方法

fragment.java

我希望从@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_fragment_1, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); height = height.findViewById(R.id.height); weight = weight.findViewById(R.id.weight); result = result.findViewById(R.id.result); calc = calc.findViewById(R.id.calc); calc.setOnClickListener(new View.OnClickListener() { String heightStr = height.getText().toString(); String weightStr = weight.getText().toString(); @Override public void onClick(View arg0) { // here you set what you want to do when user clicks your button, // e.g. launch a new activity if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) { float heightValue = Float.parseFloat(heightStr); float weightValue = Float.parseFloat(weightStr); float bmi = weightValue / (heightValue * heightValue); displayBMI(bmi); } else { result.setText(getString(R.string.Enter_height)); } } private void displayBMI(float bmi) { String bmiLabel; if (Float.compare(bmi, 15f) <= 0) { bmiLabel = getString(R.string.severely_underweight); } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) { bmiLabel = getString(R.string.very_underweight); } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) { bmiLabel = getString(R.string.underweight); } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) { bmiLabel = getString(R.string.normal_weight); } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) { bmiLabel = getString(R.string.over_weight); } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) { bmiLabel = getString(R.string.first_class_obese); } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) { bmiLabel = getString(R.string.second_class_obese); } else { bmiLabel = getString(R.string.third_class_obese); } bmiLabel = bmi + "\n" + bmiLabel; result.setText(bmiLabel); } }) ; } button: "@id calc"的{​​{1}}字段中输入的值中单击editText来计算bmi

2 个答案:

答案 0 :(得分:0)

您的代码有一些错误。在不更改代码逻辑的情况下,我已将其修复。

由于您提供的详细信息还不够,因此我不得不在这里假设一些事情,

  • 变量result在片段的XML文件中的类型为TextView,其ID为result
  • 变量heightweight在您的Fragment的XML文件中的类型为EditText(如上所述),其ID为heightweight
  • 变量calc在片段的XML文件中的类型为Button,其ID为calc
  • 您的应用程序中的Objects是什么?我看不到它的声明或在哪里初始化。除非我们知道它是什么,否则不初始化此代码将不起作用

    String heightStr;
    String weightStr;
    
    EditText height;
    EditText weight;
    TextView result;
    
    Button calc;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
    savedInstanceState) {
    
        View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);
    
        height = (EditText) view.findViewById(R.id.height);
        weight = (EditText) view.findViewById(R.id.weight);
    
        result = (TextView) view.findViewById(R.id.result);
    
        calc = (Button) view.findViewById(R.id.calc);
    
        calc.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // here you set what you want to do when user clicks your button,
                // e.g. launch a new activity
    
                heightStr = height.getText().toString();
                weightStr = weight.getText().toString();
    
                if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
                    float heightValue = Float.parseFloat(heightStr);
                    float weightValue = Float.parseFloat(weightStr);
                    float bmi = weightValue / (heightValue * heightValue);
                    displayBMI(bmi);
                } else {
                    result.setText(getString(R.string.Enter_height));
                }
            }
        });
        return view;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    
    private void displayBMI(float bmi) {
    
        String bmiLabel;
    
        if (Float.compare(bmi, 15f) <= 0) {
            bmiLabel = getString(R.string.severely_underweight);
        } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
            bmiLabel = getString(R.string.very_underweight);
        } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
            bmiLabel = getString(R.string.underweight);
        } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
            bmiLabel = getString(R.string.normal_weight);
        } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
            bmiLabel = getString(R.string.over_weight);
        } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
            bmiLabel = getString(R.string.first_class_obese);
        } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
            bmiLabel = getString(R.string.second_class_obese);
        } else {
            bmiLabel = getString(R.string.third_class_obese);
        }
    
        bmiLabel = bmi + "\n" + bmiLabel;
        result.setText(bmiLabel);
    
    }
    

答案 1 :(得分:0)

尝试在按钮单击时在Xml中添加onClick方法。

android:onClick="onButtonClick"

在片段内部创建与XML中提到的方法相同的方法。

public void onButtonClick(View view){
    //add your code here.
}