Android练手小项目-BMI计算

运行效果图:
在这里插入图片描述点击计算
在这里插入图片描述点击查看详情

重要知识点:

  1. 两个activity的跳转;
 btn3.setOnClickListener(new myListener());
 class  myListener implements  CompoundButton.OnClickListener{
        @Override
        public void onClick(View v) {
           
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,resultActivity.class);
            MainActivity.this.startActivity(intent);
        }
    }

第二个activity的返回按钮

 btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(resultActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
  1. putExtra()函数传参;getStringExtra()函数接收参数;
  2. TextView中setText()显示信息;

完整代码:
MainActivity:

public class MainActivity extends AppCompatActivity {

    private Button btn1,btn2,btn3;
    private RadioButton boy,girl;
    private EditText heightText,weightText;
    private TextView resText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //以下为变量与控件的绑定
        btn1 = (Button) findViewById(R.id.b1);
        heightText = (EditText) findViewById(R.id.H);
        weightText = (EditText) findViewById(R.id.W);
        btn2 = (Button) findViewById(R.id.b2);
        boy = findViewById(R.id.man);
        girl = findViewById(R.id.woman);
        btn3 = (Button) findViewById(R.id.go);
        btn3.setOnClickListener(new myListener());

        //button2的点击响应,作用为清空所有输入
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                heightText.setText("");
                weightText.setText("");

            }
        });
        //button2的点击事件响应,计算BMI的值,结果用Toast显示
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //得到身高体重
                String height = heightText.getText().toString();
                String weight = weightText.getText().toString();
                String msg = "";
                String bmi = "";
                double result = 0, heightNum = 0, weightNum = 0;
                if (height.isEmpty() || weight.isEmpty()) {
                    Toast.makeText(MainActivity.this, "请输入身高体重", Toast.LENGTH_SHORT).show();
                }
                if (!height.isEmpty() && !weight.isEmpty()) {
                    heightNum = Double.parseDouble(height);
                    weightNum = Double.parseDouble(weight);

                    DecimalFormat df = new DecimalFormat("#.00");
                    result = 10000 * weightNum / (heightNum * heightNum);
                    bmi = df.format(result);
                    System.out.println(bmi);
                }
                Log.i("bmi",bmi);
                msg = "您的bmi值是:".toString();
                    if (boy.isChecked()) {//如果选择的是男性
                        if (result <= 20) {
                            Toast.makeText(MainActivity.this, "您的体重过轻,吃吃吃!", Toast.LENGTH_SHORT).show();
                        } else if (result <= 25 && result > 20) {
                            Toast.makeText(MainActivity.this, "您的体重适中,继续保持!", Toast.LENGTH_SHORT).show();
                        } else if (result <= 30 && result > 25) {
                            Toast.makeText(MainActivity.this, "您的体重超重,注意减肥哦!", Toast.LENGTH_SHORT).show();
                        } else if (result < 35 && result > 30) {
                            Toast.makeText(MainActivity.this, "你的体重肥胖,马上开始减肥吧!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "您的体重严重肥胖,注意注意锻炼节食!", Toast.LENGTH_SHORT).show();
                        }

                    } else//选择的是女性
                    {
                        if (result <= 19) {
                            Toast.makeText(MainActivity.this, "您的体重过轻,吃吃吃!", Toast.LENGTH_SHORT).show();
                        } else if (result <= 24 && result > 19) {
                            Toast.makeText(MainActivity.this, "您的体重适中,继续保持!", Toast.LENGTH_SHORT).show();
                        } else if (result <= 29 && result > 24) {
                            Toast.makeText(MainActivity.this, "您的体重超重,注意减肥哦!", Toast.LENGTH_SHORT).show();
                        } else if (result < 34 && result > 29) {
                            Toast.makeText(MainActivity.this, "你的体重肥胖,马上开始减肥吧!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "您的体重严重肥胖,注意注意锻炼节食!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
        });

    }
    //查看详情页面的跳转以及信息的处理
    class  myListener implements  CompoundButton.OnClickListener{
        @Override
        public void onClick(View v) {
            //得到身高体重
            String height = heightText.getText().toString();
            String weight = weightText.getText().toString();
            String msg = "";
            String bmi = "";
            double result = 0, heightNum = 0, weightNum = 0;

            if (!height.isEmpty() && !weight.isEmpty()) {
                heightNum = Double.parseDouble(height);
                weightNum = Double.parseDouble(weight);

                DecimalFormat df = new DecimalFormat("#.00");
                result = 10000 * weightNum / (heightNum * heightNum);
                bmi = df.format(result);
                System.out.println(bmi);
            }  if (height.isEmpty() || weight.isEmpty()) {
                Toast.makeText(MainActivity.this, "请输入身高体重", Toast.LENGTH_SHORT).show();
            }
            Log.i("bmi",bmi);
            msg = "您的bmi值是:".toString();
            if (boy.isChecked()) {//如果选择的是男性
                if (result <= 20) {
                    msg = msg +bmi +"\n" +"您的体重过轻,吃吃吃!".toString();
                } else if (result <= 25 && result > 20) {
                    msg+=bmi +"\n" +"您的体重适中,继续保持!".toString();
                } else if (result <= 30 && result > 25) {
                    msg+=bmi +"\n" +"您的体重超重,注意减肥哦!".toString();
                } else if (result < 35 && result > 30) {
                    msg+=bmi +"\n" +"你的体重肥胖,马上开始减肥吧!".toString();
                } else {
                    msg+=bmi +"\n" +"您的体重严重肥胖,注意注意锻炼节食!".toString();
                }

            } else//选择的是女性
            {
                if (result <= 19) {
                    msg+=bmi +"\n" +"您的体重过轻,吃吃吃!".toString();
                } else if (result <= 24 && result > 19) {
                    msg+=bmi +"\n" +"您的体重适中,继续保持!".toString();
                } else if (result <= 29 && result > 24) {
                    msg+=bmi +"\n" +"您的体重超重,注意减肥哦!".toString();
                } else if (result < 34 && result > 29) {
                    msg+=bmi +"\n" +"你的体重肥胖,马上开始减肥吧!".toString();
                } else {
                    msg+=bmi +"\n" +"您的体重严重肥胖,注意注意锻炼节食!".toString();
                }
            }
            Intent intent = new Intent();
            intent.setClass(MainActivity.this,resultActivity.class);
            intent.putExtra("msg",msg);//将提示信息传递到result_activity
            MainActivity.this.startActivity(intent);
        }
    }
}

resultActivity:

public class resultActivity extends AppCompatActivity {
    TextView textView;
    private Button btn_back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_result);
        textView = (TextView)findViewById(R.id.bmi_result);
        Intent intent = getIntent();
        String msg = intent.getStringExtra("msg");
        textView.setText(msg);
        btn_back = (Button)findViewById(R.id.back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(resultActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_main.xml:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高(cm)"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/H"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            />
        ></LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="体重(kg)"
            android:textSize="40dp"/>
        <EditText
            android:id="@+id/W"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
        ></LinearLayout>

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="15dp">
        <RadioButton
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="30dp"
            />
        <RadioButton
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:text="女"
            android:textSize="30dp"
            />
        ></RadioGroup>
        <Button
            android:layout_marginTop="17dp"
            android:id="@+id/b1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="计算"
            android:textSize="17dp"
            />
        <Button
            android:id="@+id/b2"
            android:layout_marginTop="17dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="重置"
            android:textSize="17dp"
            />
        >

    <Button
        android:id="@+id/go"
        android:layout_marginTop="77dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看详情"/>

layout_result.xml:

    <TextView
        android:layout_marginTop="99dp"
        android:textSize="27dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bmi_result"
        />
    <Button
        android:id="@+id/back"
        android:layout_marginTop="157dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回"
     />
以下是一个简单的 Android BMI 体质指数计算器的代码示例: MainActivity.java: ```java import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private EditText heightEditText, weightEditText; private TextView resultTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); heightEditText = findViewById(R.id.heightEditText); weightEditText = findViewById(R.id.weightEditText); resultTextView = findViewById(R.id.resultTextView); } public void calculateBMI(View view) { String heightString = heightEditText.getText().toString(); String weightString = weightEditText.getText().toString(); if (heightString.isEmpty() || weightString.isEmpty()) { resultTextView.setText(R.string.empty_input_error); return; } float height = Float.parseFloat(heightString); float weight = Float.parseFloat(weightString); float bmi = calculateBMI(height, weight); String bmiCategory = getBMICategory(bmi); String result = getString(R.string.bmi_result, bmi, bmiCategory); resultTextView.setText(result); } private float calculateBMI(float height, float weight) { return weight / (height * height); } private String getBMICategory(float bmi) { if (bmi < 18.5) { return getString(R.string.underweight); } else if (bmi < 25) { return getString(R.string.normal_weight); } else if (bmi < 30) { return getString(R.string.overweight); } else { return getString(R.string.obese); } } } ``` activity_main.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/heightEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="@string/height_hint" android:layout_marginTop="32dp" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/weightEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="@string/weight_hint" android:layout_below="@id/heightEditText" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" /> <Button android:id="@+id/calculateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/calculate_button_text" android:layout_below="@id/weightEditText" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:onClick="calculateBMI" /> <TextView android:id="@+id/resultTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/result_text" android:layout_below="@id/calculateButton" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" /> </RelativeLayout> ``` strings.xml: ```xml <resources> <string name="app_name">BMI Calculator</string> <string name="height_hint">Height (m)</string> <string name="weight_hint">Weight (kg)</string> <string name="calculate_button_text">Calculate BMI</string> <string name="result_text">Your result will appear here</string> <string name="empty_input_error">Please enter your height and weight</string> <string name="bmi_result">Your BMI is %.1f, which is %s</string> <string name="underweight">underweight</string> <string name="normal_weight">normal weight</string> <string name="overweight">overweight</string> <string name="obese">obese</string> </resources> ``` 这个应用程序包含一个 EditText,用于输入身高和体重,一个 Button,用于计算 BMI,以及一个 TextView,用于显示结果。当用户点击 Calculate BMI 按钮时,它将调用 calculateBMI() 方法来计算 BMI,并使用 getBMICategory() 方法来确定 BMI 的类别。最后,它将格式化一个字符串并将其显示在 TextView 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值