成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android開發:標準體重計算器應用的開發實例

移動開發 Android
本文介紹一個簡易的標準體重計算器Android應用的開發實例。此功能在以前的手機中我們也經常看到。

應用的操作和原理

目標Android應用的操作過程是這樣的:選擇你的性別,然后輸入你的身高,點查看計算結果的按鈕就在Toast中顯示你的標準體重。力求操作簡單,結果顯示清楚。

標準體重的計算公式:

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

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

應用的源碼

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與體重計算器應用效果圖展示

#p#

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>    

應用效果圖

大家可以根據其他復雜的標準體重計算器繼續完善此應用,使其成為一個可用的、美觀的Android應用。

責任編輯:閆佳明 來源: jizhuomi
相關推薦

2022-08-09 16:01:24

應用開發鴻蒙

2016-12-12 13:41:37

iOS簡易加法開發

2009-04-02 15:58:12

AndroidEclipseSqlite

2011-09-08 13:11:07

Android Wid實例

2017-09-05 16:43:47

Electron桌面計算器

2009-12-16 10:41:47

Android日程表

2011-08-08 16:56:44

iPhone 字符處理 視圖

2013-05-20 17:51:47

Android游戲開發SurfaceView

2011-09-26 10:46:32

Android云計算開發

2013-05-20 15:42:22

2011-09-16 14:13:15

Windows7計算器

2013-05-20 17:13:17

Android游戲開發CanvasPaint

2011-07-26 11:08:23

iOS 錄像 錄音

2011-09-07 17:54:40

Android Wid開發

2013-02-20 15:29:00

JSONAndroid開發

2022-02-15 14:06:36

OpenHarmon操作系統鴻蒙

2017-07-18 14:28:04

HTMLCSSJS

2022-07-11 16:19:22

css屬性鴻蒙

2012-03-16 20:33:20

iPhone

2009-09-01 17:08:14

C#畫線控件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠骚| 91pao对白在线播放 | 伊人网国产 | 欧美操操操 | 日韩精品一区二区三区中文字幕 | 日日操av | 国产二区视频 | 久久久久久免费看 | 蜜桃精品噜噜噜成人av | 欧美视频日韩 | 国产精品久久久久久二区 | 日韩欧美在线免费 | 日韩精品一区二区三区中文在线 | 国产精品久久久久一区二区三区 | 日韩电影中文字幕在线观看 | 亚洲欧美一区二区三区国产精品 | 成人在线一级片 | 亚洲区一区二区 | 欧美一级二级三级视频 | 久久综合激情 | 国产视频观看 | 国产日韩欧美精品 | 久草精品视频 | 请别相信他免费喜剧电影在线观看 | 免费在线观看一区二区 | 亚洲在线久久 | 国产成人网 | 精品国产欧美日韩不卡在线观看 | 久久久国产精品 | 日韩在线精品视频 | 久久www免费人成看片高清 | 久久久久久免费看 | 久久国产精品视频观看 | 亚洲成人免费视频在线 | 久久小视频 | 亚洲iv一区二区三区 | 日韩精品一区二区三区 | 麻豆久久久9性大片 | 亚洲成人观看 | 我爱操 | 91精品亚洲 |