您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        这篇文章将为大家详细讲解有关AndroidStudio如何实现BMI计算,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
xml代码
```xml
<TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="30dp" android:text="BMI计算器" android:textSize="25dp"/> <EditText android:id="@+id/height" android:layout_width="match_parent" android:layout_below="@id/textView" android:layout_height="50dp" android:hint="身高(米)" android:layout_marginLeft="20dp" /> <EditText android:id="@+id/weight" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/height" android:hint="体重(公斤)" android:layout_marginLeft="20dp" /> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/weight" android:text="结论" /> <Button android:id="@+id/btn_count" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/rdgp" android:text="计算" android:onClick="ButtonClick" /> <RadioGroup android:id="@+id/rdgp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/txt"> <RadioButton android:id="@+id/rbtn_who" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WHO标准" /> <RadioButton android:id="@+id/rbtn_asia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="亚洲标准" /> <RadioButton android:id="@+id/rbtn_chn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中国标准" /> </RadioGroup>
java代码
public  void  ButtonClick(View v){
        EditText editHeight = (EditText)findViewById(R.id.height);
        EditText editWeidth = (EditText)findViewById(R.id.weight);
        TextView txtResault = (TextView)findViewById(R.id.txt);
        //获取编辑框内容,由于是字符串类型,需要转换为可计算类型
        Double height = Double.parseDouble(editHeight.getText().toString());
        Double weight = Double.parseDouble(editWeidth.getText().toString());
        //设置判断语句
        Double bmi = weight / (height*height);
/*
        BMI在18.5-24之间是正常的。
        BMI低于18.5考虑为体重过轻;
        24-27之间为超重;
        超过27以上为肥胖。
 */
        RadioButton rdbtn_1 = (RadioButton)findViewById(R.id.rbtn_who);
        RadioButton rdbtn_2 = (RadioButton)findViewById(R.id.rbtn_asia);
        RadioButton rdbtn_3 = (RadioButton)findViewById(R.id.rbtn_chn);
        //判断控件按钮是否被选中 isChecked()
        if(rdbtn_1.isChecked()){
            if(bmi<18.5){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
            }
            else  if(bmi<=24.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
            }
            else if(bmi<=29.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
            }
            else if (bmi<=34.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖!!!");
            }
            else if (bmi<=39.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重过于肥胖!!!!");
            }
            else {
                txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
            }
        }
        else if(rdbtn_2.isChecked()){
            if(bmi<18.5){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
            }
            else  if(bmi<=22.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
            }
            else if(bmi<=24.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
            }
            else if (bmi<=29.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖!!!");
            }
            else if (bmi<=40){
                txtResault.setText("BMI"+bmi.toString()+",您的体重过于肥胖!!!!");
            }
            else{
                txtResault.setText("BMI"+bmi.toString()+",您的体重严重肥胖!!!!!!!");
            }
        }
        else if (rdbtn_3.isChecked()){
            if(bmi<18.5){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏轻");
            }
            else  if(bmi<=23.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重正常");
            }
            else if(bmi<=27.9){
                txtResault.setText("BMI"+bmi.toString()+",您的体重偏重");
            }
            else {
                txtResault.setText("BMI"+bmi.toString()+",您的体重肥胖");
            }
        }
        else {
            txtResault.setText("请选择BMI标准");
        }
    }
关于“AndroidStudio如何实现BMI计算”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。