当前位置: 首页 > news >正文

移动应用开发 实验二:标准身高计算器

文章目录

  • 准备工作
  • 一,创建Android Studio项目
  • 二,创建活动模块
  • 三,设计用户界面
    • (一)设置页面布局
    • (二)添加标题文本控件
    • (三)设计体重输入框
    • (四)设计性别选项
    • (五)设计按钮和结果存放区
    • (六)运行查看效果
  • 四,编写活动代码
  • 五,运行项目


准备工作

  • 搭建开发环境
    JDK1.8,Android Studio

  • UI界面效果
    在这里插入图片描述

一,创建Android Studio项目

1,新建Android Studio项目,单击new Project。

在这里插入图片描述
2,选择phone and tablet——empty activity。

在这里插入图片描述

3,输入项目名称:HeightCalculator,语言选择Java,单击Finish。

在这里插入图片描述
4,等待下载全局配置依赖。

在这里插入图片描述5,下载完成,页面效果。
在这里插入图片描述6,单击运行按钮,启动项目,如下效果,成功创建项目。
在这里插入图片描述

二,创建活动模块

1,在项目目录右击选择——new——Activity——empty Activity。
在这里插入图片描述

2,输入名称:HeightCalculatorActivity,单击finish按钮,创建活动。
在这里插入图片描述
3,创建完成。
在这里插入图片描述

三,设计用户界面

(一)设置页面布局

1,进入code页,编写代码。
在这里插入图片描述
2,进入design页,可以使用拖动的方式添加布局方式和控件。
在这里插入图片描述
3,添加线性布局,根据效果图,页面整体布局为垂直;这里设置为垂直:android:orientation="vertical"
在这里插入图片描述

(二)添加标题文本控件

在这里插入图片描述源码:

<TextViewandroid:layout_width="410dp"android:layout_height="100dp"android:gravity="center"android:text="标准身高计算器"android:textAlignment="center"android:textSize="30sp"android:textStyle="bold" />

(三)设计体重输入框

1,根据下面效果图,这是一个水平布局,包含三个控件。
在这里插入图片描述
2,首先,先添加一个水平布局,android:orientation="horizontal",表示水平布局。
在这里插入图片描述

3,在水平布局下添加文本视图控件(相当于标签,显示文本)。
在这里插入图片描述
4,添加一个输入框控件,用于接收用户输入的数据,并且给上一个id值,后续编写Java代码接收信息。
在这里插入图片描述5,最后再添加一个文本视图控件,用于显示最后的单位。
在这里插入图片描述

(四)设计性别选项

1,分析:从下图中也可发现这是一个水平布局,并且两个单选按钮是绑定在一起的(如不是共同,则性别就可以全选,就失去了单选按钮的特点,出现逻辑错误)。
在这里插入图片描述
2,根据分析,此时再添加一个水平布局,二级的布局方式,与体重输入框布局属于同级。
在这里插入图片描述

<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:orientation="horizontal"></LinearLayout>

3,添加文本视图控件。
在这里插入图片描述

<TextViewandroid:layout_width="150.0dip"android:layout_height="wrap_content"android:layout_gravity="center"android:text="请选择你的性别:"android:textSize="18sp" />

4,添加单选控件组,将两个单选按钮绑定到一起。
在这里插入图片描述

<RadioGroupandroid:id="@+id/RadioGroup01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"></RadioGroup>

5,在控件组中添加两个单选按钮控件。
在这里插入图片描述

<RadioButton                       android:id="@+id/man"          android:layout_width="50.0dip" android:layout_height="70.0dip"android:checked="true"         android:text="" />            <RadioButton                       android:id="@+id/woman"        android:layout_width="50.0dip" android:layout_height="70.0dip"android:text="" />            

(五)设计按钮和结果存放区

1,添加水平布局,在布局下添加按钮控件。
在这里插入图片描述

<LinearLayout                               android:layout_width="fill_parent"      android:layout_height="wrap_content"    android:gravity="center_horizontal"     android:orientation="horizontal">       <Button                                 android:id="@+id/calculator"        android:layout_width="200.0dip"     android:layout_height="wrap_content"android:layout_marginTop="20.0dip"  android:text="运算 " />               
</LinearLayout>                             

2,添加水平布局,在其中添加文本视图控件用于存放结果(添加id)。
在这里插入图片描述

<LinearLayout                                android:layout_width="fill_parent"       android:layout_height="wrap_content"     android:gravity="center_horizontal"      android:orientation="horizontal">        <TextView                                android:id="@+id/result"             android:layout_width="wrap_content"  android:layout_height="wrap_content" android:layout_marginTop="10.0dip" />
</LinearLayout>                              

(六)运行查看效果

1,右击身高计算器的Java文件,单击运行按钮。
在这里插入图片描述
2,弹出如下提示,无法运行(原因是没有配置运行配置文件:AndroidManifest.xml)。
在这里插入图片描述
3,打开AndroidManifest.xml,设置android:exported="true",在单击运行按钮。
在这里插入图片描述
4,模拟器显示出正常页面,没有什么问题。
在这里插入图片描述

四,编写活动代码

编写HeightCalculatorActivity.java,实现计算标准身高。
在这里插入图片描述

package com.example.heightcalculator;import androidx.appcompat.app.AppCompatActivity;import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;public class HeightCalculatorActivity extends AppCompatActivity {private Button calculatorButton;private EditText weightEditText;private RadioButton manRadioButton;private RadioButton womanRadioButton;private TextView resultTextView;private static final int EXIT=1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_height_calculator);calculatorButton=(Button)findViewById(R.id.calculator);weightEditText=(EditText)findViewById(R.id.weight);manRadioButton=(RadioButton)findViewById(R.id.man);womanRadioButton=(RadioButton)findViewById(R.id.woman);resultTextView=(TextView)findViewById(R.id.result);}@Overrideprotected void onStart() {super.onStart();registerEvent();}private void registerEvent(){calculatorButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(!weightEditText.getText().toString().trim().equals("")){double weight=Double.parseDouble(weightEditText.getText().toString());StringBuffer sb=new StringBuffer();sb.append("------------评估结果----------- \n");if(manRadioButton.isChecked()){sb.append("男性标准身高:");double result=evaluateHeight(weight,"男");sb.append((int)result+"(厘米)");}else if(womanRadioButton.isChecked()){sb.append("女性标准身高:");double result=evaluateHeight(weight,"女");sb.append((int)result+"(厘米)");}resultTextView.setText(sb.toString());}else{showMessage("请输入体重!");}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, EXIT, Menu.NONE, "退出");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId()==EXIT){finish();//退出程序}return super.onOptionsItemSelected(item);}private double evaluateHeight(double weight,String sex){double height;if(sex=="男"){height=170-(62-weight)/0.6;}else{height =158-(52-weight)/0.5;}return height;}private void showMessage(String message){AlertDialog alert = new AlertDialog.Builder(this).create();alert.setTitle("系统信息");alert.setMessage(message);alert.setButton("确定", (dialog, whichButton) -> {});alert.show();}}

五,运行项目

1,单击运行按钮。
在这里插入图片描述
2,启动成功。在这里插入图片描述

3,输入体重,得出结果。
在这里插入图片描述

http://www.lryc.cn/news/478325.html

相关文章:

  • 金华迪加现场大屏互动系统 mobile.do.php 任意文件上传漏洞复现
  • 使用 pd.ExcelWriter 创建多工作表 Excel 文件的详细教程
  • 驱动-----dht11温湿度传感器
  • Docker 基础命令简介
  • 嵌入式开发之静态库和共享库
  • 关于npm源的切换及相关操作
  • vue前端sku实现
  • 使用Vue3和Vue2进行开发的区别
  • 爬虫入门urllib 和 request(二)
  • 【大数据学习 | HBASE】hbase的整体架构
  • 群控系统服务端开发模式-应用开发-个人资料
  • openssl生成加密,公钥实现非对称加密
  • [CKS] K8S Admission Set Up
  • 前端学习Day13 CSS盒子的定位(固定定位篇“附练习”)
  • Tomcat 启动卡住,日志显示 At least one JAR was scanned for TLDs yet contained no TLDs.
  • 计算机网络:网络层 —— 移动 IP 技术
  • useCrudSchemas
  • SpringBoot3集成Junit5
  • 【EMNLP2024】阿里云人工智能平台 PAI 多篇论文入选 EMNLP2024
  • Spark的Shuffle过程
  • Java+Swing可视化图像处理软件
  • RDD转换算子:【mapValues、mapPartitions】
  • 数组和指针的复杂关系
  • Linux系统I/O调优实例
  • 记录Ubuntu OS的异常
  • Vue 3 单元测试与E2E测试
  • 猫用空气净化器哪个牌子好?求除毛好、噪音小的宠物空气净化器!
  • 第十九课 Vue组件中的方法
  • 【JavaScript】V8,Nodejs 与浏览器
  • 内存马浅析