开发者社区> 问答> 正文

代码不适用于bmi计算。只有一个代码块有效,而另一个不起作用

当我单击第二个微调器项目时,该特定微调器项目的代码应执行但未执行。如果正在执行部分,则仅执行一个

我尝试了以下代码

package com.example.bmicalculator;

import android.graphics.Color; import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.shashank.sony.fancytoastlib.FancyToast;

import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar;

import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity { Spinner gender_spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);

    // Get the references to the widgets
    final EditText weight_et = (EditText) findViewById(R.id.weight_et);
    final EditText height_et = (EditText) findViewById(R.id.height_et);
    final EditText height_et1 = (EditText) findViewById(R.id.ft_in_et);
    final EditText weight_et1 =(EditText)findViewById(R.id.st_lb_et);
    final TextView result_tv = (TextView) findViewById(R.id.result_tv);
    final TextView ideal_weight = (TextView)findViewById(R.id.ideal_weight);


    final Spinner height_spinner = findViewById(R.id.height_spinner);
    ArrayAdapter<CharSequence> adapter_height = ArrayAdapter.createFromResource(this,R.array.Height,android.R.layout.simple_spinner_item);
    adapter_height.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    height_spinner.setAdapter(adapter_height);

    height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    height_et.setHint("Height(cm)");
                    height_et1.setVisibility(View.GONE);break;
                case 1:
                    height_et1.setHint("in");
                    height_et1.setVisibility(View.VISIBLE);
                    height_et.setHint("ft");break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

            FancyToast.makeText(getApplicationContext(),"Error",FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
        }
    });




    final Spinner weight_spinner= findViewById(R.id.weight_spinner);
    ArrayAdapter<CharSequence> adapter_weight = ArrayAdapter.createFromResource(this,R.array.Weight,android.R.layout.simple_spinner_item);
    adapter_weight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    weight_spinner.setAdapter(adapter_weight);

    weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    weight_et.setHint("Weight(kg)");
                    weight_et1.setVisibility(View.GONE);break;
                case 1:
                    weight_et.setHint("Weight(lb)");
                    weight_et1.setVisibility(View.GONE);break;
                case 2:
                    weight_et.setHint("st");
                    weight_et1.setVisibility(View.VISIBLE);
                    weight_et1.setHint("lb");break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

            FancyToast.makeText(getApplicationContext(),"Error",FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
        }
    });


    gender_spinner = findViewById(R.id.gender_spinner);
    ArrayAdapter<CharSequence> adapter_gender = ArrayAdapter.createFromResource(this,R.array.Gender,android.R.layout.simple_spinner_item);
    adapter_gender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    gender_spinner.setAdapter(adapter_gender);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            weight_et.setText("");
            height_et.setText("");
            height_et1.setText("");
            height_et1.setVisibility(View.GONE);
            weight_et1.setText("");
            weight_et1.setVisibility(View.GONE);
            result_tv.setText("");
            ideal_weight.setText("");

            height_spinner.setSelection(0);
            weight_spinner.setSelection(0);
        }
    });


    if (height_spinner.getSelectedItemPosition()==0 && weight_spinner.getSelectedItemPosition()==0){

        findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {

            // Logic for validation, input can't be empty
            @Override
            public void onClick(View v) {

                String Height = height_et.getText().toString().trim();
                String Weight = weight_et.getText().toString().trim();

                if(Height.isEmpty()){
                    height_et.setError("Please enter your Height");
                    height_et.requestFocus();
                    return;
                }

                if(Weight.isEmpty()){
                    weight_et.setError("Please enter your Weight");
                    weight_et.requestFocus();
                    return;
                }

//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float height = Float.parseFloat(Height)/100;

//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight

//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);

                if (bmiValue<16) {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >=16 && bmiValue<=17){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>17 && bmiValue <18.5) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>=18.5 && bmiValue <= 25) {
                    result_tv.setTextColor(Color.parseColor("#008000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
                }
                else if ( bmiValue > 25 && bmiValue < 30) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 30 && bmiValue <35){
                    result_tv.setTextColor(Color.parseColor("#FF4500"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 35 && bmiValue <40){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }


            }
        });

    }

    if (height_spinner.getSelectedItemPosition()==1 && weight_spinner.getSelectedItemPosition()==0){

        findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {

            // Logic for validation, input can't be empty
            @Override
            public void onClick(View v) {

                String Height = height_et.getText().toString().trim();
                String Weight = weight_et.getText().toString().trim();
                String Height1 = height_et1.getText().toString().trim();

                if(Height.isEmpty()){
                    height_et.setError("Please enter feet");
                    height_et.requestFocus();
                    return;
                }

                if(Weight.isEmpty()){
                    weight_et.setError("Please enter your Weight(in kg)");
                    weight_et.requestFocus();
                    return;
                }

                if (Height1.isEmpty()){
                    height_et1.setError("Please Enter inches");
                    height_et1.requestFocus();
                    return;
                }

//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float feet = Float.parseFloat(Height); float inches = Float.parseFloat(Height1); float height = convertftandin(feet,inches)/100;

//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight

//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);

                if (bmiValue<16) {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >=16 && bmiValue<=17){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>17 && bmiValue <18.5) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>=18.5 && bmiValue <= 25) {
                    result_tv.setTextColor(Color.parseColor("#008000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
                }
                else if ( bmiValue > 25 && bmiValue < 30) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 30 && bmiValue <35){
                    result_tv.setTextColor(Color.parseColor("#FF4500"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 35 && bmiValue <40){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }


            }
        });

    }





}

//Calculate BMI
private float calculateBMI (float weight, float height) {
    return (float) (weight / (height * height));
}


//conversion of ft and in to cm

private float convertftandin(float ft,float in){
    return ((ft*30.48f)+(in*2.54f));
}

//Calculate Ideal Weight


// Interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Very Severely Underweight";
    }
    else if (bmiValue >=16 && bmiValue<=17){
        return "Severely Underweight";
    }
    else if (bmiValue>17 && bmiValue <18.5) {
        return "Underweight";
    }
    else if (bmiValue>=18.5 && bmiValue <= 25) {

        return "Normal";
    }
    else if ( bmiValue > 25 && bmiValue < 30) {

        return "Overweight";
    }
    else if (bmiValue >= 30 && bmiValue <35){
        return "Obese Class I";
    }
    else if (bmiValue >= 35 && bmiValue <40){
        return "Obese Class II";
    }
    else {
        return "Obese Class III";
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_remove_ads) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

} 当我单击第二个微调器项目时,应执行该特定微调器项目的代码,但未执行。只有一部分正在执行时, 以下代码不起作用

if (height_spinner.getSelectedItemPosition()==1 && weight_spinner.getSelectedItemPosition()==0){

        findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {

            // Logic for validation, input can't be empty
            @Override
            public void onClick(View v) {

                String Height = height_et.getText().toString().trim();
                String Weight = weight_et.getText().toString().trim();
                String Height1 = height_et1.getText().toString().trim();

                if(Height.isEmpty()){
                    height_et.setError("Please enter feet");
                    height_et.requestFocus();
                    return;
                }

                if(Weight.isEmpty()){
                    weight_et.setError("Please enter your Weight(in kg)");
                    weight_et.requestFocus();
                    return;
                }

                if (Height1.isEmpty()){
                    height_et1.setError("Please Enter inches");
                    height_et1.requestFocus();
                    return;
                }

//Get the user values from the widget reference float weight = Float.parseFloat(Weight); float feet = Float.parseFloat(Height); float inches = Float.parseFloat(Height1); float height = convertftandin(feet,inches)/100;

//Calculate BMI value float bmiValue = calculateBMI(weight, height); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String rounded_bmivalue= decimalFormat.format(bmiValue); //Calculate Ideal Weight

//Define the meaning of the bmi value String bmiInterpretation = interpretBMI(bmiValue);

                if (bmiValue<16) {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >=16 && bmiValue<=17){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>17 && bmiValue <18.5) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue>=18.5 && bmiValue <= 25) {
                    result_tv.setTextColor(Color.parseColor("#008000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));
                }
                else if ( bmiValue > 25 && bmiValue < 30) {
                    result_tv.setTextColor(Color.parseColor("#9ACD32"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 30 && bmiValue <35){
                    result_tv.setTextColor(Color.parseColor("#FF4500"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else if (bmiValue >= 35 && bmiValue <40){
                    result_tv.setTextColor(Color.parseColor("#FF6347"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }
                else {
                    result_tv.setTextColor(Color.parseColor("#FF0000"));
                    result_tv.setText(String.valueOf(rounded_bmivalue + " - " + bmiInterpretation));

                    ideal_weight.setTextColor(Color.parseColor("#008000"));
                }


            }
        });```

展开
收起
小六码奴 2019-10-09 18:13:29 1153 0
1 条回答
写回答
取消 提交回答
  • 要R.id.ibl根据选择的Spinner 设置onClickListener 。

    但是,执行此操作的部分不在Spinner的侦听器内部。这就是为什么它们不会在每次单击特定微调器时执行。

    您的代码仅执行下面的代码块,if (height_spinner.getSelectedItemPosition()==0 && weight_spinner.getSelectedItemPosition()==0)因为它们是微调器的初始值。

    您可能需要将代码块设置为Spinners' onClickListener的R.id.ibl内部onItemSelectedListener。

    2019-10-09 18:16:18
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载