歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發應用實例:計算標准體重的實例(簡單版)

Android開發應用實例:計算標准體重的實例(簡單版)

日期:2017/3/1 10:36:31   编辑:Linux編程
下面是一個簡單的計算標准體重的實例,選擇自己的性別,再輸入自己的身高,點擊Button就能在Toast顯示自己的標准體重,看看自己的體重有沒有符合標准哦。

計算標准體重的方法:

男性:(身高cm-80)×70﹪=標准體重 女性:(身高cm-70)×60﹪=標准體重

背景圖片下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/1月/29日/Android開發應用實例:計算標准體重的實例(簡單版)/

BMIActivity.java

  1. package com.lingdududu.bmi;
  2. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.RadioButton;
  11. import android.widget.Toast;
  12. /*
  13. * @author lingdududu * 該程序的功能是用戶選擇自己的性別和輸入自己的身高,然後點擊按鈕,就能在Toast顯示出自己的標准體重
  14. */
  15. public class BMIActivity extends Activity {
  16. /** Called when the activity is first created. */
  17. private Button countButton;
  18. private EditText heighText;
  19. private RadioButton maleBtn, femaleBtn;
  20. String sex = "";
  21. double height;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. //調用創建視圖的函數
  27. creadView();
  28. //調用性別選擇的函數
  29. sexChoose();
  30. //調用Button注冊監聽器的函數
  31. setListener();
  32. }
  33. //響應Button事件的函數
  34. private void setListener() {
  35. countButton.setOnClickListener(countListner);
  36. }
  37. private OnClickListener countListner = new OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. // TODO Auto-generated method stub
  41. Toast.makeText(BMIActivity.this, "你是一位"+sexChoose()+"\n"
  42. +"你的身高為"+Double.parseDouble(heighText.getText().toString())+"cm"
  43. +"\n你的標准體重為"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
  44. .show();
  45. }
  46. };
  47. //性別選擇的函數
  48. private String sexChoose(){
  49. if (maleBtn.isChecked()) {
  50. sex = "男性";
  51. }
  52. else if(femaleBtn.isChecked()){
  53. sex = "女性";
  54. }
  55. return sex;
  56. }
  57. //創建視圖的函數
  58. public void creadView(){
  59. //txt=(TextView)findViewById(R.id.txt);
  60. countButton=(Button)findViewById(R.id.btn);
  61. heighText=(EditText)findViewById(R.id.etx);
  62. maleBtn=(RadioButton)findViewById(R.id.male);
  63. femaleBtn=(RadioButton)findViewById(R.id.female);
  64. //txt.setBackgroundResource(R.drawable.bg);
  65. }
  66. //標准體重格式化輸出的函數
  67. private String format(double num) {
  68. NumberFormat formatter = new DecimalFormat("0.00");
  69. String str = formatter.format(num);
  70. return str;
  71. }
  72. //得到標准體重的函數
  73. private String getWeight(String sex, double height) {
  74. height = Double.parseDouble(heighText.getText().toString());
  75. String weight = "";
  76. if (sex.equals("男性")) {
  77. weight =format((height - 80) * 0.7);
  78. }
  79. else {
  80. weight = format((height - 70) * 0.6);
  81. }
  82. return weight;
  83. }
  84. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@drawable/pic"
  7. >
  8. <TextView
  9. android:id="@+id/txt"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center"
  13. android:text="@string/hello"
  14. android:textSize="16px"
  15. />
  16. <TextView
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="@string/sex"
  20. />
  21. <RadioGroup
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="horizontal"
  25. >
  26. <RadioButton
  27. android:id="@+id/male"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="男"
  31. />
  32. <RadioButton
  33. android:id="@+id/female"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="女"
  37. />
  38. </RadioGroup>
  39. <TextView
  40. android:layout_width="fill_parent"
  41. android:layout_height="26px"
  42. android:text="@string/heigh"
  43. />
  44. <EditText
  45. android:id="@+id/etx"
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content"
  48. />
  49. <Button
  50. android:id="@+id/btn"
  51. android:layout_width="fill_parent"
  52. android:layout_height="wrap_content"
  53. android:text="@string/count"
  54. />
  55. </LinearLayout>

效果圖:

650) this.width=650;">

Copyright © Linux教程網 All Rights Reserved