Android之BMI体脂计算器
MainActivity
package com.example.graceto.shiyan3;import android.content.Intent;
import android.icu.text.DecimalFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {RadioGroup rg;String sex;Button button1;EditText height,weight;RadioButton boy,girl;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
// RadioGroup rg= (RadioGroup) findViewById(R.id.radiogroup1);//获取单选按钮的值button1 = (Button) findViewById(R.id.button1);height = (EditText) findViewById(R.id.height);//获取编辑框数据weight = (EditText) findViewById(R.id.weight);boy = (RadioButton) findViewById(R.id.boy);//获取性别girl = (RadioButton) findViewById(R.id.girl);//j计算按钮事件button1.setOnClickListener(new View.OnClickListener() {@RequiresApi(api = Build.VERSION_CODES.N)@Overridepublic void onClick(View v) {clickbutton();}});}//计算按钮事件@RequiresApi(api = Build.VERSION_CODES.N)public void clickbutton() {
// RadioGroup rg= (RadioGroup) findViewById(R.id.radiogroup1);//获取单选按钮的值//获取文本数据String h = height.getText().toString();String w = weight.getText().toString();String msg = "";String bmi = "";//初始化身高体重double res = 0,heightNum = 0,weightNum = 0;if(!h.isEmpty()&&!w.isEmpty()) {heightNum = Double.parseDouble(h); // 强制类型转换,把括号里的类型变成doubleweightNum = Double.parseDouble(w);res = 10000 * weightNum / (heightNum * heightNum);//保留两位小数DecimalFormat df = new DecimalFormat("#.00");bmi = df.format(res);
// System.out.println();}Log.i("bmi",bmi);
// msg ="你的BMI值是".toString();if (boy.isChecked()){if(res>35){msg="严重肥胖,控制饮食,合理运动";}else if(res>30){msg="肥胖,建议少吃点东西,多锻炼身体";}else if(res>25){msg="超重,建议少吃点东西,多锻炼身体";}else if(res>20){msg="适中,继续保持";}else {msg="过轻,建议多吃点东西";}}if (girl.isChecked()){if(res>34){msg="严重肥胖,控制饮食,合理运动";}else if(res>29){msg="肥胖,建议少吃点东西,多锻炼身体";}else if(res>24){msg="超重,建议少吃点东西,多锻炼身体";}else if(res>19){msg="适中,继续保持";}else {msg="过轻,建议多吃点东西";}}Intent intent = new Intent(MainActivity.this,Main2.class);//构建一个实例化对Bundle bundle = new Bundle();bundle.putCharSequence("bmi",bmi);//保存数据bundle.putCharSequence("msg",msg);//保存数据intent.putExtras(bundle);//保存到intent中startActivity(intent);//实现了把数据传递到第二个activity中}}
Main2
package com.example.graceto.shiyan3;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;/*** Created by grace to on 2020/5/14.*/public class Main2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second);Intent intent = getIntent();//获取intent对象Bundle bundle = intent.getExtras();//存储传过来的buddleString BMI = (String) bundle.get("bmi");//获取传过来的bmi,存储为字符串String MSG = (String) bundle.get("msg");//获取传过来的bmi,存储为字符串TextView BM = findViewById(R.id.Text2);//获取文本框TextView JY = findViewById(R.id.Text4);//创建显示意图BMI=intent.getStringExtra("bmi");BM.setText(BMI);JY.setText(MSG);//返回页面Button btn1 = (Button) findViewById(R.id.button2);//获取按钮btn1.setOnClickListener(new View.OnClickListener(){Intent intent = null;public void onClick(View v){//添加单击事件监听器//第一种返回方式:
// Intent intent = new Intent(SecondActivity.this,MainActivity.class);//构建一个实例化对象
//
// startActivity(intent);//实现了把数据传递到第二个activity中//第二种:直接关掉即可finish();}});}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="@drawable/image1"tools:context="com.example.graceto.shiyan3.MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="测测你的BMI"android:textSize="60sp"android:textColor="#FFA07A"android:textStyle="bold"android:shadowRadius="3.0"android:shadowColor="#F9F900"android:shadowDx="10.0"android:shadowDy="10.0"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="身 高(cm)"android:textColor="#FFD39B"android:textSize="30dp"android:layout_marginLeft="20dp"android:layout_marginTop="20dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:theme="@style/MyEditText"android:id="@+id/height"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:hint="请输入"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="number"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="体 重(kg)"android:textColor="#FFD39B"android:textSize="30dp"android:layout_marginLeft="20dp"android:layout_marginTop="20dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:theme="@style/MyEditText"android:id="@+id/weight"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:hint="请输入"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="number"/><RadioGroupandroid:id="@+id/radiogroup1"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="-125dp"><RadioButtonandroid:id="@+id/boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:textColor="#FFD39B"android:textSize="30dp"/><RadioButtonandroid:id="@+id/girl"android:textColor="#FFD39B"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:textSize="30dp"/></RadioGroup><Buttonandroid:id="@+id/button1"android:background="#FFD39B"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="-62dp"android:text="计算BMI"android:textSize="30dp"/>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/puzzle2"tools:context="com.example.graceto.shiyan3.MainActivity"><TextViewandroid:id="@+id/bmi"android:layout_weight="2"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="33sp"android:textColor="#000000"android:layout_margin="10dp"android:text="BMI:" /><TextViewandroid:id="@+id/Text2"android:layout_marginLeft="10px"android:layout_marginTop="-10dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="40sp"android:textColor="#FFD39B" /><TextViewandroid:id="@+id/jy"android:layout_weight="1"android:gravity="center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="33sp"android:textColor="#000000"android:layout_margin="5dp"android:text="身体状态:" /><TextViewandroid:id="@+id/Text4"android:layout_marginTop="-20dp"android:layout_marginLeft="10px"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="40sp"android:textColor="#FFD39B" /><Buttonandroid:id="@+id/button2"android:background="#FFD39B"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="42dp"android:layout_marginLeft="20dp"android:layout_marginBottom="12dp"android:text="返回"android:textSize="30dp"/></LinearLayout>
androidmanifes:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.graceto.shiyan3"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".Main2"></activity></application></manifest>