Android's simple BMI calculator (use two interfaces Jump display)

BMI Calculator

This use to jump between Activity, each app typically consists of multiple interfaces, each interface is a activity, interface jump is a jump between activity.
Activity Intent with the jump between the (intended) component
Intent assembly is intended, each of the program components is an important way to interact, not only the current component can specify actions to be performed, may also be data transmission between the different components .
Intent is divided into explicit and implicit intent intent

Explicit intent

Explicit intent can be turned directly by specifying the name of the target component, by a method configured
to achieve Intent (ContextpackageContext, Class <?> Cls), wherein the first parameter is the current activity objects, this can be used, the second parameter start the target activity.
Create a target activity by this method, the object is then passed to the activity of startActivity (Intent, intent) method to start the target assembly.
Sample code is as follows:

Intent intent = new Intent(this,Activity2.class);   //创建Intent对象
startActivity(intent);  //开启Activity2

Implicit intent

Display intention implicit intent is relatively more abstract, it does not clearly indicate which components you want to open the target, but the system is analyzed based on the information specified by the action attribute information and category, etc., and then find the target Activity.

The following sample code

Intent intnet = new Intent();
//设置activity动作,该动作要和清单文件中的设置一样
intent.setAction("cn.itcast.START_ACTIVITY");
startActivity(intent);  

The above code, did not specify the category, because the category configuration in the target Activity in the manifest file is a default value when calling startActivity () method, automatically add this category to the Intent.
We need to configure the target list file, specify the action to respond to the current Activity and category

The following sample code

activity android:name="cn,itcast.Activity2"
	<intent-filter>
	<!--设置action属性,需要在代码中根据所设置的name打开指定组件-->
	<action android:name="cn.itcast.START_ACTIVITY"/>
	<category android:name="android.intent.category.DEFAULT"/>
	</intent-filter>
</activity>

Note: Each Intent can specify only one action, but can specify more than one category

The next step is to do with the intent of BMI calculator

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="   身高(米):" />

    <EditText
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入身高:" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="   体重(公斤):" />

    <EditText
        android:id="@+id/weight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入体重:" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:layout_margin="30dp"
        android:text="计算体质指数" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="24sp"
        android:textColor="#03A9F4"
        android:text="         清   除        " />
</LinearLayout>

1 shows the results interface
Interface 1

MainActivity.java

package com.example.bmi;  //项目包名,一般不用赋值,建项目自动生成的
 
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    Button button1, button2;
    EditText heightText, weightText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        heightText = findViewById(R.id.height);
        weightText = findViewById(R.id.weight);

        //j计算按钮事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    clickbutton();
            }
        });
        //清除按钮事件
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                weightText.setText("");
                heightText.setText("");
            }
        });
    }
    //计算按钮事件
    public void clickbutton() {
        String h=heightText.getText().toString();
        String w=weightText.getText().toString();
        //判断输入内容是否为空
        if(h==null||w==null){
            Toast.makeText(MainActivity.this,"提醒:输入为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //初始化身高体重
        double dh=1,dw=1,BMI;
        try {
            //把tv字符串转化为double并赋值
            dh=Double.parseDouble(h);
            dw=Double.parseDouble(w);
        } catch (Exception e) {
            Toast.makeText(MainActivity.this,"提醒:输入非法",Toast.LENGTH_SHORT).show();
            return;
        }
        BMI = dw / (dh * dh);
        //保留两位小数
        DecimalFormat nf = new DecimalFormat("0.00");
        String BMIText1=nf.format(BMI);
        //传递参数到第二个页面
        Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        //intent.setClass(this,Main2Activity.class);
        intent.putExtra("BMI", BMIText1);
        startActivity(intent);


    }
}

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context="com.example.bmi.Main2Activity" >

    <ImageView
        android:id="@+id/tp"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:src="@drawable/tu1"
        />
    <TextView
        android:id="@+id/bmi"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#E25A8B"
        android:layout_margin="10dp"
        android:text="BMI:" />
    <TextView
        android:id="@+id/Text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4" />
    <TextView
        android:id="@+id/jy"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#EE66A2"
        android:layout_margin="5dp"
        android:text="身体状态:" />
    <TextView
        android:id="@+id/Text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#03A9F4" />
</LinearLayout>

Code interpretation section:
Android: the src = "@ the drawable / TU1" // add pictures in the image view of the assembly

2 shows the results interface
2 interface

Main2Activity.java

package com.example.bmi;  //项目包名

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    String zt,sjy;
    String sbmi;
    double dbmi=1;
    TextView BMIText, classificationText,bmi,jy;
    ImageView tp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        bmi=(TextView) findViewById(R.id.bmi);
        jy=(TextView) findViewById(R.id.jy);
        tp=(ImageView) findViewById(R.id.tp);
        BMIText = findViewById(R.id.Text2);
        classificationText = findViewById(R.id.Text4);
        //创建显示意图
        Intent intent=getIntent();
        sbmi=intent.getStringExtra("BMI");
        dbmi=Double.valueOf(sbmi);
        BMIText.setText("您的BMI:"+dbmi);
        //判断
        tp.setImageResource(R.drawable.tu1);
        if(dbmi<=18.4){
            classificationText.setText("偏瘦,建议多吃点东西");
        }
        else if(dbmi<=23.9){
            classificationText.setText("正常,继续保持");
        }
        else if(dbmi<=27.9){
            classificationText.setText("过重,建议少吃点东西,多锻炼身体");
        }
        else {
            classificationText.setText("肥胖,建议少吃多运动");
        }

    }
}

Run results show:
Results 1
Results
Come on!

Released five original articles · won praise 6 · views 447

Guess you like

Origin blog.csdn.net/qq_45844792/article/details/105033615