1、
MainActivity.java
1 package example.bmi; 2 3 import java.text.DecimalFormat; 4 5 import android.net.Uri; 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.app.AlertDialog; 9 import android.content.ClipData.Item; 10 import android.content.DialogInterface; 11 import android.content.Intent; 12 import android.view.Menu; 13 import android.view.MenuItem; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.TextView; 19 20 @SuppressWarnings("unused") 21 public class MainActivity extends Activity { 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 findViews(); 29 setListensers(); 30 } 31 private Button button_calc; 32 private EditText field_height; 33 private EditText field_weight; 34 // private TextView view_result; 35 // private TextView view_suggest; 36 private void findViews() 37 { 38 button_calc=(Button)findViewById(R.id.submit); 39 field_height=(EditText)findViewById(R.id.height); 40 field_weight=(EditText)findViewById(R.id.weight); 41 // view_result=(TextView)findViewById(R.id.result); 42 // view_suggest=(TextView)findViewById(R.id.suggest); 43 } 44 private void setListensers() 45 { 46 button_calc.setOnClickListener(calcBMI); 47 } 48 private Button.OnClickListener calcBMI=new Button.OnClickListener() 49 { 50 public void onClick(View v) 51 { 52 //switch to report page 53 Intent intent = new Intent(); 54 intent.setClass(MainActivity.this, Report.class); 55 startActivity(intent); 56 Bundle bundle = new Bundle(); 57 bundle.putString("KEY_HEIGHT", field_height.getText().toString()); 58 bundle.putString("KEY_WEIGHT", field_weight.getText().toString()); 59 intent.putExtras(bundle); 60 startActivity(intent); 61 } 62 }; 63 protected static final int MENU_ABOUT = Menu.FIRST; 64 protected static final int MENU_Quit = Menu.FIRST+1; 65 66 @Override 67 public boolean onCreateOptionsMenu(Menu menu) 68 { 69 super.onCreateOptionsMenu(menu); 70 menu.add(0, MENU_ABOUT, 0, "关于"); 71 menu.add(0, MENU_Quit, 0, "结束"); 72 return true; 73 } 74 public boolean onOptionsItemSelected(MenuItem item) 75 { 76 super.onOptionsItemSelected(item); 77 switch(item.getItemId()) 78 { 79 case MENU_ABOUT: 80 openOptionsDialog(); 81 break; 82 case MENU_Quit: 83 finish(); 84 break; 85 } 86 return true; 87 } 88 private void openOptionsDialog() 89 { 90 new AlertDialog.Builder(MainActivity.this) 91 .setTitle(R.string.about_title) 92 .setMessage(R.string.about_msg) 93 .setPositiveButton(R.string.ok_label, 94 new DialogInterface.OnClickListener() { 95 public void onClick( 96 DialogInterface dialoginterface,int i){ 97 } 98 99 }) 100 .setNegativeButton(R.string.homepage_label, 101 new DialogInterface.OnClickListener() { 102 103 @Override 104 public void onClick(DialogInterface dialog, int which) { 105 Uri uri=Uri.parse(getString(R.string.homepage_uri)); 106 Intent intent=new Intent(Intent.ACTION_VIEW,uri); 107 startActivity(intent); 108 } 109 }) 110 .show(); 111 } 112 113 /* @Override 114 public boolean onCreateOptionsMenu(Menu menu) { 115 // Inflate the menu; this adds items to the action bar if it is present. 116 getMenuInflater().inflate(R.menu.activity_main, menu); 117 return true; 118 } 119 */ 120 }
2、
report.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 > 7 8 <TextView android:id="@+id/result" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="" 12 /> 13 <TextView android:id="@+id/suggest" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="" 17 android:layout_below="@id/result" 18 /> 19 <Button android:id="@+id/report_back" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="@string/report_back" 23 android:layout_below="@id/suggest" 24 /> 25 </RelativeLayout>
3、
Report.java
1 package example.bmi; 2 3 import java.text.DecimalFormat; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.widget.Button; 7 import android.widget.TextView; 8 import android.view.View; 9 10 11 public class Report extends Activity { 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.report); 16 17 findViews(); 18 setListeners(); 19 showResults(); 20 } 21 private Button button_back; 22 private TextView view_result; 23 private TextView view_suggest; 24 25 private void findViews() 26 { 27 button_back=(Button)findViewById(R.id.report_back); 28 view_result=(TextView)findViewById(R.id.result); 29 view_suggest=(TextView)findViewById(R.id.suggest); 30 } 31 private void setListeners(){ 32 button_back.setOnClickListener(backMain); 33 } 34 private Button.OnClickListener backMain = new Button.OnClickListener() 35 { 36 public void onClick(View v) 37 { 38 Report.this.finish(); 39 } 40 }; 41 private void showResults() 42 { 43 DecimalFormat nf=new DecimalFormat("0.00"); 44 45 Bundle bundle = this.getIntent().getExtras(); 46 double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100; 47 double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT")); 48 double BMI = weight/(height*height); 49 view_result.setText(getString(R.string.bmi_result)+nf.format(BMI)); 50 //give health advice 51 if(BMI>25) 52 { 53 view_suggest.setText(R.string.advice_heavy); 54 } 55 else if(BMI<20) 56 { 57 view_suggest.setText(R.string.advice_light); 58 } 59 else 60 { 61 view_suggest.setText(R.string.advice_average); 62 } 63 } 64 }