AndroidStudio实训1——BMI计算器

一些xml中的标签定义

一、 AndroidStudio中的线性布局(LinearLayout)多用于按钮的水平或垂直排列
二、LinearLayout常用属性

  1. orientation:布局中组件的排列方式
    android:orientation=“vertical”(组件垂直排列)
    android:orientation=“horizontal”(组件水平排列)

2、gravity:控制组件所包含的子元素的对齐方式
3、**layout_gravity:**控制该组件在父容器中的对其方式
4、layout_width:布局的宽度,一般不直接写数字
通常android:layout_width=“match_parent”(填满容器的意思)
**layout_height:**布局的高度,
通常android:layout_height=“match_parent”
5、id:为该组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件
6、background:为该组件设置一个背景图片,或者直接用颜色覆盖
7、wrap_content:和组件本身大小适配
match_parent:和组件适配

三、TextView控件基本属性:
TextView控件就是可以写在中的
1、**layout_width=“match_parent”**控件的宽度是适应父容器的
layout_height=“wrap_content”,控件的高度与控件本身大小适配

2、TextView中也有:
id
background
gravity
text

四、EditText是一个常用的控件,也是一个比较重要的组件,是用户跟安卓应用进行数据传输的窗户
1、ems用法:
android:ems=“10”,设置TextView或EditText的宽度为10个字符的宽度

五、BMI的整体代码:
1、BMI计算器的布局文件,
activity_main.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!--更改布局为线性布局LinearLayout
     水平布局:
     垂直布局:vertical
     -->
<!--这个最外面的Linearout是整个大页面的布局-->
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"

    >


    <!--第一组身高组件-->
    <!--wrap_content:和组件本身大小适配-->
    <!--match_parent:和父窗口适配-->
    <!-- 下边这个TextView是第一行 “身高”这两个字的布局  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="身高"
        />

    <!--EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,
    可以说它是用户跟Android应用进行数据传输的窗户-->
    <!--android:ems ="10"设置TextView或者Edittext的宽度为10个字符的宽度-->

    <!-- 下面这个EditView是用户输入身高为多少的那个横线区域   -->
    <EditText
        android:id="@+id/shengao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        />

    <!--第二组体重组件-->
    <!-- 下边这个TextView是第一行 “体重”这两个字的布局  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:text="体重"
        />

    <!-- 下面这个EditView是用户输入体重为多少的那个横线区域   -->
    <EditText
        android:id="@+id/tizhong"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        />

    <!--剩下的一个按钮“计算”-->
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="计算"


        />

    <!--剩下的另一个按钮“重置”-->
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="重置"


        />

</LinearLayout>

视图如下:
在这里插入图片描述

2、BMI的java代码,是写在MainActivity中的:
这里补充Activity的生命周期:
它的的四个阶段:
(1)开始Activity:在这个阶段依次执行3个生命周期方法:onCreate()、onStart()和onResume()。
(2)Activity失去焦点:如果在Activity获得焦点的情况下进入其他的Activity,这时当前的Activity会失去焦点。在这一阶段,会依次执行onPause()和onStop()方法。
(3)Activity重新获得焦点:如果Activity重新获得焦点,会依次执行 3个生命周期方法:onRestart()、onStart()和onResume()。
(4)关闭Activity:当Activity被关闭时系统会依次执行3个生命周期方法:onPause()、onStop()和onDestroy()。
(5)七个生命周期方法如下,它们的作用见下面示例方法注释中

protected void onCreate(Bundle savedInstanceState)

   protected void onStart()//当Activity第一次被创建时调用,我们可以在这进行初始化操作

   protected void onResume()

   protected void onPause()

   protected void onStop()

   protected void onRestart()

   protected void onDestroy()  

(6)MainActivity.java的代码

package cn.tedu.bmi;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    
    
    private EditText shengao,tizhong;
    private Button jisuan,quxiao;
    //接收身高体重的基本数据类型
    private double height,weight=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);//调用父类的onCreate构造函数,
        setContentView(R.layout.activity_main);//设置当前的Activity显示的内容按activity_main.xml布局
        //通过id来获取控件对象
        shengao=(EditText) findViewById(R.id.shengao);
        tizhong=(EditText) findViewById(R.id.tizhong);
        jisuan=(Button) findViewById(R.id.btn1);
        quxiao=(Button) findViewById(R.id.btn2);

        //为“计算”按钮添加监听事件
        jisuan.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
            //首先需要去判断两个输入框不为空
                if(shengao.getText()==null&&shengao.getText().toString().trim().equals("")){
    
    
                    shengao.setError("身高不能为空!");
                    return;

                }
                if(tizhong.getText()==null&&tizhong .getText().toString().trim().equals("")){
    
    
                    shengao.setError("体重不能为空!");
                    return;

                }
                //当身高输入框中的内容获取不为空
                if(shengao.getText()!=null&&!shengao.getText().toString().trim().equals("")){
    
    
                   //把获取到输入框中的基本内容赋值给基本数据类型
                    height=Double.parseDouble(shengao.getText().toString());

                }
                //当体重输入框中的内容获取不为空
                if(tizhong.getText()!=null&&!tizhong.getText().toString().trim().equals("")){
    
    
                    //把获取到输入框中的基本内容赋值给基本数据类型
                    height=Double.parseDouble(tizhong.getText().toString());

                }

                //BMI计算公式:BMI=体重/身高/身高  (体重:kg,身高:m)
                double bmi=weight*5000/height/height;//这里输入的体重是瑾,身高是cm
                if(bmi<18){
    
    
                    //偏瘦

                    Toast.makeText(
                            MainActivity.this,
                            "您的身材偏瘦,请增加营养摄入",
                            Toast.LENGTH_LONG
                    ).show();
                }else if(bmi >=20&&bmi<=25){
    
    
                    //标准
                    Toast.makeText(
                            MainActivity.this,
                            "您的身材标准,请保持身材",
                            Toast.LENGTH_LONG
                    ).show();
                }else if(bmi<30){
    
    
                    //偏胖
                    Toast.makeText(
                            MainActivity.this,
                            "您的身材偏瘦,请增加营养摄入",
                            Toast.LENGTH_LONG
                    ).show();
                }else{
    
    
                    //肥胖
                    Toast.makeText(
                            MainActivity.this,
                            "您的身材偏胖,请增加身体锻炼",
                            Toast.LENGTH_LONG
                    ).show();
                }
            }
        });

        //为“重置“按钮添加监听事件
        quxiao.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                //不想计算了,点击之后,清空输入框中的内容
                shengao.setText(" ");
                tizhong.setText(" ");
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_46161529/article/details/128486697
今日推荐