实验任务2 标准体重计算器
来源:互联网 发布:怎么注册淘宝云客服 编辑:程序博客网 时间:2024/04/20 07:51
一、实验效果图:
二、主要代码:
1、java代码:
(1)MainActivity.java
package com.example.demo08;import android.app.Activity;import android.content.Intent;import android.os.Bundle;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;public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.btnClick);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Info info = new Info();int etHeight = Integer.parseInt(((EditText) findViewById(R.id.etHeight)).getText().toString());RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1);for (int i = 0; i < sex.getChildCount(); i++) {RadioButton r = (RadioButton) sex.getChildAt(i);if (r.isChecked()) {info.setSex(r.getText().toString());break;}}info.setStature(etHeight);Bundle bundle = new Bundle();bundle.putSerializable("info", info);Intent intent = new Intent(MainActivity.this,ShowActivity.class);intent.putExtras(bundle);startActivity(intent);}});}}
package com.example.demo08;import java.text.DecimalFormat;import java.text.NumberFormat;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ShowActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_show);TextView sex = (TextView) findViewById(R.id.sex);TextView etHeight = (TextView) findViewById(R.id.etHeight);TextView weight = (TextView) findViewById(R.id.weight);Intent intent = this.getIntent();Bundle bundle = intent.getExtras();Info info = (Info) bundle.getSerializable("info");sex.setText("你是一位" + info.getSex() + "性");etHeight.setText("你的身高是" + info.getStature() + "厘米");weight.setText("你的标准体重是:" + getWeight(info.getSex(), info.getStature())+ "公斤");}private String getWeight(String sex, int etHeight) {String weight = "";NumberFormat format = new DecimalFormat();if (sex.equals("男")) {weight = format.format((etHeight - 80) * 0.7);} else {weight = format.format((etHeight - 70) * 0.6);}return weight;}}
package com.example.demo08;import java.io.Serializable;public class Info implements Serializable {private static final long serialVersionUID = 1L;private String sex = "";private int stature = 0;public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getStature() {return stature;}public void setStature(int stature) {this.stature = stature;}}
(1)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout1" 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/textShowTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="计算你的标准体重" android:textSize="40px" /> <TextView android:id="@+id/tvSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textShowTitle" android:layout_marginTop="30dp" android:text="性别:" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tvSex" android:layout_centerHorizontal="true" android:orientation="horizontal" > <RadioButton android:id="@+id/remale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tvSex" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/refemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/remale" android:layout_alignBottom="@+id/remale" android:layout_toRightOf="@+id/tvSex" android:text="女" /> </RadioGroup> <TextView android:id="@+id/tvWeight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/tvSex" android:layout_below="@+id/tvSex" android:layout_marginTop="39dp" android:text="身高:" /> <EditText android:id="@+id/etHeight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tvWeight" android:layout_alignBottom="@+id/tvWeight" android:layout_alignParentRight="true" android:background="@android:drawable/edit_text" android:ems="10" android:selectAllOnFocus="true" /> <Button android:id="@+id/btnClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="计算" /></RelativeLayout>
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="null" android:textSize="10pt" /> <TextView android:id="@+id/etHeight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="null" android:textSize="10pt" /> <TextView android:id="@+id/weight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="null" android:textSize="10pt" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo08" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demo08.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.demo08.ShowActivity" android:label="@string/title_activity_show" > </activity> </application></manifest>
1、RadioButton组件的使用,使用这个组件时要用到RadioGroup,才能达到单选按钮的效果;
2、Intent的使用,实现数据在两个活动之间的传输,且不出错;
3、判断性别是男还是女,从而运用不同的计算公式来计算人的体重。
0 0
- 实验任务2 标准体重计算器
- 实验二 任务2 标准体重计算器
- 实验2 Activity和Intent-任务2:标准体重计算器
- 实验二—任务2:标准体重计算器--------------闪退
- 任务2---标准体重计算器
- 实验二.任务二.标准体重计算器
- 实验二任务二标准体重计算器
- 实验二_任务二_标准体重计算器
- 实验1 标准体重计算器
- 实验1:标准体重计算器
- 任务二 标准体重计算器
- 任务二:标准体重计算器
- 实验二 Intent 简单标准体重计算器
- android实验二之体重标准计算器
- 初学移动开发技术_实验二_任务二_标准体重计算器
- 实验二——任务二——标准体重计算器(出错待改)
- C++实验2-标准体重
- c++实验2-标准体重
- 递归解决汉诺塔
- 哈希表VS红黑树
- POJ 2524
- python问题:IndentationError:expected an indented block错误
- Netty学习笔记<1>--UNIX的5种I/O模型
- 实验任务2 标准体重计算器
- STM32F103RCT6的脉冲宽度调制(PWM)的输出设定
- 在CentOS7下的gdb中run出现的Missing separate debuginfos, use: debuginfo-install
- bzoj2002(lct)
- 如何实现1080P延迟低于500ms的实时超清直播传输技术
- js获取get参数
- 4070: [Apio2015]雅加达的摩天楼
- 双重检查加锁,实现单例模式
- Dex文件及Dalvik字节码格式解析