android 的簡單程式碼 解釋

程式碼如下 因為是初學 所以希望可以逐句翻譯 謝謝

package com.android.demo.bmi;import java.text.DecimalFormat;import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;public class Bmi extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//Listen for button clicks

Button button = (Button)findViewById(R.id.submit);

button.setOnClickListener(calcBMI);

}

private OnClickListener calcBMI = new OnClickListener() {

public void onClick(View v) {

DecimalFormat nf = new DecimalFormat("0.00");

EditText fieldheight = (EditText)findViewById(R.id.height);

EditText fieldweight = (EditText)findViewById(R.id.weight);

double height = Double.parseDouble(fieldheight.getText().toString())/100;

double weight = Double.parseDouble(fieldweight.getText().toString());

double BMI = weight / (height * height); TextView result = (TextView)findViewById(R.id.result);

result.setText("Your BMI is "+nf.format(BMI)); //Give health advice

TextView fieldsuggest = (TextView)findViewById(R.id.suggest);

if(BMI>25) {

fieldsuggest.setText(R.string.advice_heavy);

} else if(BMI<20) {

fieldsuggest.setText(R.string.advice_light);

} else {

fieldsuggest.setText(R.string.advice_average);

}

}

};

}

2 Answers

Rating
  • 9 years ago
    Favorite Answer

    import java.text.DecimalFormat;import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;//

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.TextView;--------------以上是需要用的類別----------------------------

    public class Bmi extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);//引用main.xml

    -----------以上程式進入點---------------

    //Listen for button clicks

    Button button = (Button)findViewById(R.id.submit);//宣告 一個按鈕

    button.setOnClickListener(calcBMI); //把按鈕函式(clacBMI)交給OnClickListener方法處理 }

    private OnClickListener calcBMI = new OnClickListener() {

    public void onClick(View v) {

    DecimalFormat nf = new DecimalFormat("0.00");//數值格式

    EditText fieldheight = (EditText)findViewById(R.id.height);//宣告可輸入身高變數,從main.xml中的<EditText >@height引入,以下同理

    EditText fieldweight = (EditText)findViewById(R.id.weight);//宣告可輸入體重變數

    double height = Double.parseDouble(fieldheight.getText().toString())/100;//轉成字串儲存成double 型態

    double weight = Double.parseDouble(fieldweight.getText().toString());//轉成字串儲存成double 型態

    double BMI = weight / (height * height);計算BMI TextView result = (TextView)findViewById(R.id.result);宣告結果變數

    result.setText("Your BMI is "+nf.format(BMI));顯示計算結果 //Give health advice

    TextView fieldsuggest = (TextView)findViewById(R.id.suggest);//宣告結果變數

    if(BMI>25) {

    fieldsuggest.setText(R.string.advice_heavy);

    } else if(BMI<20) {

    fieldsuggest.setText(R.string.advice_light);//顯示建議

    } else {

    fieldsuggest.setText(R.string.advice_average);//顯示建議

    }

    }

    };

    }

  • 6 years ago

    下面的網址應該對你有幫助

    http://phi008780508.pixnet.net/blog

Still have questions? Get your answers by asking now.