失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 根据身高体重计算标准身高和BMI指数

根据身高体重计算标准身高和BMI指数

时间:2022-03-06 03:31:42

相关推荐

根据身高体重计算标准身高和BMI指数

记录贴,一个小白的安卓学习之路

```javapackage com.example.applicationfirst;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.TextView;import android.widget.Toast;//事件监听才用自身类作为事件监听器//(1)让当i请安活动类实现事件监听//(2)重写对应的方法//(3)给控件指定监听事件public class MainActivity extends AppCompatActivity implements View.OnClickListener{//1、声明控件public TextView tv_result;public EditText et_inputweight,et_inputheight;public RadioButton rb_man,rb_woman;public Button btn_cancle,btn_check;//2、获取控件//3、事件处理//4、菜单处理@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getView();btn_cancle.setOnClickListener(this);btn_check.setOnClickListener(this);}public void getView(){tv_result=findViewById(R.id.tv_result);et_inputweight=findViewById(R.id.et_inputweight);rb_man=findViewById(R.id.rb_man);rb_woman=findViewById(R.id.rb_woman);btn_cancle=findViewById(R.id.bt_cancle);btn_check=findViewById(R.id.bt_check);et_inputheight=findViewById(R.id.et_inputheight);}@Overridepublic void onClick(View view) {double bmi=0;double height=0;switch (view.getId()){case R.id.bt_cancle:Toast.makeText(this,"点击取消按钮",Toast.LENGTH_SHORT).show();et_inputweight.setText("");et_inputheight.setText("");tv_result.setText("您的BMI指数为:");break;case R.id.bt_check://获取用户输入的体重//判断体重输入框是否为空值String w=et_inputweight.getText().toString();String h=et_inputheight.getText().toString();if(w.equals("")|h.equals("")){Toast.makeText(this,"请输入体重 KG,身高 CM",Toast.LENGTH_SHORT).show();}else {double weight=Double.valueOf(et_inputweight.getText().toString());double hight=Double.valueOf(et_inputheight.getText().toString());//判断性别if(rb_man.isChecked()){height=170-(62-weight)/0.6;bmi =weight/(hight*hight*0.01*0.01);if(bmi<=18.4){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi) +"\n建议:"+"您的体型偏瘦,您应适当补充营养");}else if (bmi>18.5&&bmi<23.9){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型正常,请注意适当运动保持健康");} else if (bmi>24.0&&bmi<27.9){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型偏胖,体重过重,应注意健康饮食适当瘦身");} else if (bmi>=28.0){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n"+"您的体型肥胖,应适当运动减肥,注意健康饮食");}else {tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi));}}else if(rb_woman.isChecked()){height=158-(52-weight)/0.5;bmi =weight/(hight*hight*0.01*0.01);//BIM指数=体重/(身高*身高)//if(bim<=18.4){// tv_result.setText("您偏瘦")}else if(bim>18.5||bim<23.9)zhengchangif(bmi<=18.4){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型偏瘦,您应适当补充营养");}else if (bmi>18.5&&bmi<23.9){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型正常,请注意适当运动保持健康");} else if (bmi>24.0&&bmi<27.9){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型偏胖,体重过重,应注意健康饮食适当瘦身");} else if (bmi>=28.0){tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi)+"\n建议:"+"您的体型肥胖,应适当运动减肥,注意健康饮食");}else {tv_result.setText("您的标准身高为:"+String.format("%.2f",height)+"CM\n"+"您的BMI指数为:"+String.format("%.2f",bmi));}}}break;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE,1,1,"退出");return true;}@Overridepublic boolean onOptionsItemSelected(@NonNull MenuItem item) {switch ( item.getItemId()){case 1:finish();break;}return true;}}

如果觉得《根据身高体重计算标准身高和BMI指数》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。