編輯src→com.demo.android.bmi→Main.java
完整API查詢:http://developer.android.com/reference/packages.html
package com.demo.android.bmi; import java.text.DecimalFormat; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; 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; import android.widget.Toast; public class Main extends Activity { private Button button_calc; private EditText field_height; private EditText field_weight; private TextView view_result; private TextView view_good_weight; private TextView view_suggest; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findviews(); setListensers(); } private void findviews() { button_calc = (Button)findViewById(R.id.submit); field_height=(EditText)findViewById(R.id.height); field_weight=(EditText)findViewById(R.id.weight); view_result=(TextView)findViewById(R.id.result); view_suggest=(TextView)findViewById(R.id.suggest); view_good_weight=(TextView)findViewById(R.id.goodWeight); } private void setListensers() { button_calc.setOnClickListener(calcBMI); } private OnClickListener calcBMI = new OnClickListener(){ public void onClick(View v){ DecimalFormat nf = new DecimalFormat("0.0"); try{ double height = Double.parseDouble( field_height.getText().toString() ) / 100; double weight = Double.parseDouble( field_weight.getText().toString() ); double BMI=weight / ( height * height ); view_result.setText( getText( R.string.bmi_result ) + nf.format(BMI) ); double min_weight=20 * ( height * height ); double max_weight=25 * ( height * height ); view_good_weight.setText(getText(R.string.goodWeight) + nf.format(min_weight) +"~" + nf.format(max_weight)); //建議 if(BMI > 25){ view_suggest.setText(R.string.advice_heavy); }else if(BMI < 20){ view_suggest.setText(R.string.advice_light); }else{ view_suggest.setText(R.string.advice_average); } openOptionsDialog(); }catch(Exception obj){ //隱現視窗,LENGTH_SHORT短訊息 Toast.makeText(Main.this, R.string.errMsg1, Toast.LENGTH_SHORT).show(); } } private void openOptionsDialog() { //建立具名實體,會佔記憶體到關閉程式為止 Main.this 是指建立在目前畫面之上 /* AlertDialog.Builder dialog = new AlertDialog.Builder(Main.this); dialog.setTitle("關於BMI App"); dialog.setMessage("這只是我練習的一個小App啦~"); dialog.show(); */ //建立對話框匿名實體,結束就釋放記憶體,適合短暫秀出 new AlertDialog.Builder(Main.this) .setTitle(R.string.aboutTitle) .setMessage(R.string.aboutContent) .setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton(R.string.label_homepage, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //連到某個網址,地圖可用 geo: 經度 , 緯度,電話tel:2130669 // Uri.parse不支援資源識別符號,所以用getString()取得實際值 Uri uri = Uri.parse( getString( R.string.label_homepage_url ) ); //"意圖"實體,ACTION_VIEW 根據不同檔案類型,自動執行關聯軟體 //ACTION_DIAL可撥電話 Intent intent = new Intent(Intent.ACTION_VIEW , uri); //執行意圖 startActivity(intent); } }) .show(); } }; }