2018年9月19日 星期三

Android Studio撰寫程式第一課

        目前所使用的版本為Android Studio 3.1.4,而在一般所找到的網路大部份,教初學者撰寫第一隻基本的應用程式,則為"BMI的計算方式....!!!
        
       以下為BMI的公式-->BMI=體重(公斤)/身高公尺2
         範例:
    假設一個人的體重為88公斤,身高為158公分 那套上以上的公式計算一下。

      88/158*158=88/2.4964      計算出來的結果為BMI-->35.12
  
  在Android Studio 3.1.4的版本中有與個地方需特別的注意:

  JAVA的App中原始的程式碼如下:

  

   

  上圖所顯示的為用來將所需的函式庫給放入到JAVA中的MainActivity.java中。


下面的程式碼為參考網路上的相關資料撰寫而成的,而這部份只局限於JAVA中的MainActivity.java中。


package aixstudio.bmi_example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;


public class MainActivity extends AppCompatActivity {
     TextView tmy;               //宣告全域變數
     EditText h;                //宣告全域變數 
     EditText w;                //宣告全域變數 
   @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setTitle("BMI  Example");                 //取得標題名稱
        setContentView(R.layout.activity_main);                          //設定Layout的群組

        h = (EditText)findViewById(R.id.Et1);                           // 取得身高物件
        w = (EditText)findViewById(R.id.Et2 );                           // 取得體重物件
        Button submit = (Button)findViewById(R.id.Btn1 );             // 取得按鈕物件 
              如果ID有誤請回到res->Layout查看預設Button是多少
              // 按下按鈕 觸發事件
        submit .setOnClickListener(new Button.OnClickListener(){
            @Override
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) +"Kg/m2。");           // 顯示BMI計算結果
                    tmy= (TextView)findViewById(R.id.Tv4);// 取得 顯示診斷 物件
                                       // 診斷結果 顯示                    

                    if (fresult<18.5)

                         tmy.setText("你的身材過輕,需多吃一點。");

                    else if (18.5 <= fresult && fresult< 24)

                        tmy.setText("你的身材在正常範圍,可以繼續維持下去。");

                    else if (24 <=fresult && fresult < 27)

                        tmy.setText("你的身材過重,而開始注意飲食,並且多運動。");

                    else if (27 <=fresult && fresult < 30)

                        tmy.setText("你的身材輕度肥胖,需配合醫師,並且多運動。");

                    else if (30 <= fresult && fresult < 35)

                        tmy.setText("你的身材中度肥胖,需配合醫師,並且多運動。");

                    else if (fresult >= 35)

                        tmy.setText("你的身材重度肥胖,需配合醫師,並且多運動。");




                }
            }
        } );
    }
}


  Xml的App中的Layout圖如下:
   選擇Layout的物件請統一放置在Component Tree中,便可對所使用的物件做有效的應用。

  
  下圖為Layout出的圖型。


  下圖為使用模擬器所使用的結果。









沒有留言:

張貼留言