前言
如果還沒安裝好Android模擬器可參考【Android Studio安裝教學】【Eclipse安裝】,雖然這邊提供兩個IDE安裝方法,但還是建議大家安裝Android Studio。
對於第一次開發Android的初學者來說,我覺得直接做一個簡單專案最有效果,以下教學不一定要全部都懂,只要知道畫面如何製作,程式要寫在哪邊這樣就夠了。
Android Studio看這篇【撰寫第一支APP 計算BMI值 從1到2 (Android Studio)】
1.設計BMI值手機介面
所有手機畫面都會放在 Android Project → res → layout底下,每個XML都代表一個介面。
首先到Android Project → res → layout 點選 activity_main.xml(名稱依據建立Project時命名為主),找到Palette視窗裡面有很多元件提供你設計畫面,找到需求的元件後拖拉到右方手機畫面。
我們先拉4個Medium Text至手機畫面,負責顯示文字。

我們拖拉4個Medium Text至手機畫面,分別修改4個Medium Text的ID跟Text,由上到下分別為(Id/Texy):tv1/身高 tv2/體重 tv3/結果 tv4/診斷


接著再拉兩個Plain Text至身高、體重旁邊,修改這兩個Plain Text的ID分別為et1跟et2,接著設定et1跟et2的Input Type = number 限制輸入數字


再拉一個按鈕Button ID不變 Text改為「送出」

可點選手機預覽畫面的下方,activity_main.xml查看原始碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<RelativeLayout 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: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/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="身高:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv1" android:layout_below="@+id/tv1" android:layout_marginTop="15dp" android:text="體重:" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/tv1" android:layout_marginLeft="15dp" android:layout_toRightOf="@+id/tv1" android:ems="10"/> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et1" android:layout_below="@+id/et1" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/et2" android:layout_below="@+id/et2" android:layout_marginTop="21dp" android:text="送出" /> <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:text="結果:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/tv3" android:layout_marginTop="10dp" android:text="診斷:" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout> |
2.撰寫BMI值功能
我們進入到專案src底下點選*.java檔開始撰寫程式

程式碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import java.text.NumberFormat; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText h; //宣告全域變數 EditText w; //宣告全域變數 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); h = (EditText)findViewById(R.id.et1); // 取得身高物件 w = (EditText)findViewById(R.id.et2); // 取得體重物件 Button submit = (Button)findViewById(R.id.button1); // 取得按鈕物件 // 按下按鈕 觸發事件 submit.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { //判斷條件 身高 跟 體重 都有輸入值才執行 if ( !("".equals(h.getText().toString()) || "".equals(w.getText().toString())) ) { float fh = Float.parseFloat(h.getEditableText().toString()); // 取得 身高輸入值 float fw = Float.parseFloat(w.getEditableText().toString()); // 取得 體重輸入值 float fresult; // BMI值 計算結果 TextView result = (TextView)findViewById(R.id.tv3);// 取得 顯示結果 物件 fh = fh/100; // 計算BMI fh = fh*fh; // 計算BMI NumberFormat nf = NumberFormat.getInstance(); // 數字格式 nf.setMaximumFractionDigits(2); // 限制小數第二位 fresult = fw/fh; // 計算BMI result.setText(nf.format(fw/fh) +""); // 顯示BMI計算結果 TextView dia = (TextView)findViewById(R.id.tv4);// 取得 顯示診斷 物件 // 診斷結果 顯示 if (fresult<18.5) dia.setText("體重過輕"); else if (18.5 <= fresult && fresult< 24) dia.setText("正常範圍"); else if (24 <=fresult && fresult < 27) dia.setText("過 重"); else if (27 <=fresult && fresult < 30) dia.setText("輕度肥胖"); else if (30 <= fresult && fresult < 35) dia.setText("中度肥胖"); else if (fresult >= 35) dia.setText("重度肥胖 "); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |
4.執行結果畫面

Exp Up
Intent + Bundle 切換Activity並傳值 從2到3 – 介面換頁並傳值
請問版主~在哪裡執行.結果畫面 呢? 因為目前使用的Android Studio版本在按鈕上還有一些位置差異很大,希望版主可以再針對新版本發新的教學,萬分感謝~
你可以先參考Android Studio安裝教學把模擬器叫出來,至於最後執行在IDE上方有個綠色箭頭應該很好找。
很感謝你的意見我會找時間針對Studio在發一篇教學
先感謝版主了,已跪!
請問 這段程式碼 有甚麼用途呢?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
我把它隱藏之後 程式可以正常使用
onCreateOptionsMenu 用於你有使用Menu功能時,它負責初始化Menu選項。
此範例沒有用到Menu功能所以可以把這個方法刪除,因當初新增Layout系統自己產生沒有特別整理。
請問出現以下error會是甚麼問題呢???
Error:(7, 28) No resource found that matches the given name (at ‘paddingBottom’ with value ‘@dimen/activity_vertical_margin’).
妳res\values\dimen 資料夾底下缺少一個activity_vertical_margin.xml檔案