Android开发实现的标准体重计算器功能示例
作者:初雪海蓝 发布时间:2023-01-19 02:56:48
标签:Android,计算器
本文实例讲述了Android开发实现的标准体重计算器功能。分享给大家供大家参考,具体如下:
运行结果界面:
界面设计
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="68dp"
android:text="@string/sex" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/textView1" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex1" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex2" />
</RadioGroup>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerVertical="true"
android:text="@string/high" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/TextView01"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="72dp"
android:layout_toRightOf="@+id/textView2"
android:onClick="ClickHandler"
android:text="@string/calculate" />
</RelativeLayout>
实现功能:
package org.wwj.calculate;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义各个组件
private Button cal;
private EditText high;
private RadioGroup sex;
private RadioButton sex1;
private RadioButton sex2;
private Double weight;
String sex3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过findViewById获取对象
cal=(Button) this.findViewById(R.id.button1);
high=(EditText) this.findViewById(R.id.editText1);
sex=(RadioGroup) this.findViewById(R.id.radioGroup1);
sex1=(RadioButton) this.findViewById(R.id.radio0);
sex2=(RadioButton) this.findViewById(R.id.radio1);
//设置Button事件监听
cal.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/*设置事件监听*/
sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//事件处理
if(checkedId==sex1.getId()){
weight=(Double.parseDouble(high.getText().toString()) - 80) * 0.7;
sex3="男性";
}else if(checkedId==sex2.getId()){
weight=(Double.parseDouble(high.getText().toString()) - 70) * 0.6;
sex3="女性";
}
}
});
/*Toast显示内容*/
Toast.makeText(MainActivity.this, "你是一位"+sex3+"\n"
+"你的身高是"+high.getText().toString()+"\n"+"你的标准体重是"+weight,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/juan_love/article/details/8758827


猜你喜欢
- 背景数据库在保存数据时,对于某些敏感数据需要脱敏或者加密处理,如果一个一个的去加显然工作量大而且容易出错,这个时候可以考虑使用 * ,本文针
- Select Count(*)的返回值类型<select id="queryAlarmStatisticalAnalysis
- spring Session 提供了一套用于管理用户 session 信息的API和实现。Spring Session为企业级Java应用的
- 本文研究的主要是Java回调函数与观察者模式的实现,具体介绍和实现代码如下。观察者模式(有时又被称为发布(publish )-订阅(Subs
- 在android平台上的app,在主页面时经常会遇到“再按一次退出app”的功能,避免只按一下返回键就退出app提升体验优化。1、这个功能需
- 本篇给大家介绍Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图。静态资源访问在我们开发Web应用的时候
- 文件比较平常都是用Beyond Compare,可以说离不开的神器,特别是针对代码比较这块,确实挺好用的。不过Beyond Compare平
- 目录前言RenderObject 类继承层级解析RenderBox叶节点与父节点控件的测量与布局performResize 和 perfor
- Python是一种广泛使用的解释型、高级编程、通用型编程语言,由吉多·范罗苏姆创造,第一版发布于1991年。可以视之为一种改良(加入一些其他
- 本文以实例形式介绍了基于Java实现的Dijkstra算法,相信对于读者研究学习数据结构域算法有一定的帮助。Dijkstra提出按各顶点与源
- 本文实例讲述了Android编程四大组件之BroadcastReceiver(广播接收者)用法。分享给大家供大家参考,具体如下:这里介绍如何
- 引言朋友要查看一个已存在包的versionCode信息,在群里面询问,由于是已经存在的apk包,并且只需要其中的versionCode信息,
- 什么是包包 (package) 是组织类的一种方式.使用包的主要目的是保证类的唯一性.例如, 你在代码中写了一个 Test 类. 然后你的同
- Java是一门面向对象的语言,即一切皆是对象!那么为何数据类型中还分为:基本类型和对象?Java中有8种基本数据类型boolean、byte
- 访问控制:private 私有的protected 受保护的public 公共的类、方法和变量修饰符abstract 声明抽象class 类
- Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置:在JAVA程序上右键--> Ru
- 本文实例为大家分享了Java文件操作的具体代码,供大家参考,具体内容如下简介本程序主要采用了FileInputStream和FileOutp
- 本文实例讲述了Java8新增的重复注解功能。分享给大家供大家参考,具体如下:一 点睛在Java 8以前,同一个程序元素前最多只能使用一个相同
- 本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的List
- 策略模式所谓策略其实就是做一件事情有很多很多的方法,比如说一个商场要搞促销,促销的方式有可能有很多:打折啊,满100返50啊、积分等等之类的