Initial Android development experience-BMI. Calc

Source: Internet
Author: User

These two days, I finally made up my mind to look at the book. After setting up the development environment, I came into contact with the first hello World Android beginner demo, I have a rough understanding of eclipse-based Android programming framework.

The book used is the Google Android development Getting Started Guide (second edition) published by the People's post and telecommunications Publishing House. I would like to thank the BUPT library.
Cainiao took the first step!

Development notes:

V1.8.6.3.40

Feature update:

1. Add menu

2. Modify the dialog box and one-click Weibo login function to move to the menu

3. added the program exit function in the menu.

4. Modify the calculation result and suggest moving it to the next panel

5. Status Bar prompt function during overweight

V1.8.5.10.52

Feature update:

1. User prompt when the input box is empty

2. added the dialog box function for button calling.

3. added the one-click Weibo login function

V1.8.4.21.11

Test Platform: android1.5 hvga

Features:

1. Calculate the physical index of ordinary people and give suggestions

Java source code

V1.8.6.3.40

//Bmi.java
Package COM. demo. android. BMI; import java.net. urisyntaxexception; 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. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import and Roid. widget. button; import android. widget. edittext; import android. widget. textview; import android. widget. toast; public class BMI extends activity {/** called when the activity is first created. * // @ override // rewrite the public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // call setcontentview (R. layout. main); // display it on the screen // listen for Button Clicks // obtain the button component by using the findviewbyid method. The description type is consistent and mandatory conversion 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 "); // define double-precision floating point number * try {* edittext fieldheight = (edittext) findviewbyid (R. id. height); // get the number * edittext fieldweight = (edittext) findviewbyid (R. id. weight); // returns the number * double H. Eight = 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 body mass index BMI value:" + NF. format (BMI); // * Here I started to use "@ string/bmi_result", and then use R. string. bmi_result, the results are not good. It is estimated that there is a relationship with the + number ** textview fieldsuggest = (Te Xtview) findviewbyid (R. id. suggest); // * obtain the control 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) ;}} catch * (exception OBJ) {toast. maketext (BMI. this, "It's really proud. You have to fill both empty spaces! ", * Toast. length_long ). show (); // automatically hide the bread, prompt friendly} * // click the button to calculate the BMI value for the first version of the second version above. // click the button for the third version below to go to the report page edittext field_height = (edittext) findviewbyid (R. id. height); // get the number of edittext field_weight = (edittext) findviewbyid (R. id. weight); // get the number if ("". equals (field_height.gettext (). tostring () | "". equals (field_weight.gettext (). tostring () {toast. maketext (BMI. this, "Don't be proud, you have to fill both empty! ", Toast. length_long ). show () ;}else {intent = new intent (); intent. setclass (BMI. this, report. class); bundle = new bundle (); bundle. putstring ("key_height", field_height.gettext (). tostring (); bundle. putstring ("key_weight", field_weight.gettext (). tostring (); intent. putextras (bundle); startactivity (intent) ;}}; protected static final int menu_about = menu. first; protected static final int menu_quit = menu. first + 1; @ overridepublic Boolean oncreateoptionsmenu (menu) {// create a menu // todo auto-generated method stubsuper. oncreateoptionsmenu (menu); menu. add (0, menu_about, 0, "about... "). seticon (Android. r. drawable. ic_menu_info_details); menu. add (0, menu_quit, 0, "quit "). seticon (Android. r. drawable. ic_menu_close_clear_cancel); return Super. oncreateoptionsmenu (menu) ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// process menu option action // todo auto-generated method stubswitch (item. getitemid () {Case menu_about: openoptionsdialog (); break; Case menu_quit: Finish (); break;} return Super. onoptionsitemselected (item);} private void openoptionsdialog () {New alertdialog. builder (BMI. this ). settitle (R. string. about_title) // set the title. setmessage (R. string. about_msg) // set the content. setpositivebutton (R. string. OK _label, new dialoginterface. onclicklistener () {public void onclick (dialoginterface, int I) {// function body is empty, click to close}) // Add confirmation button. setnegativebutton (R. string. homepage_label, new dialoginterface. onclicklistener () {public void onclick (dialoginterface, int I) {URI uri = Uri. parse (getstring (R. string. homepage_uri); // note: the URL here must contain http: //; otherwise, the focus of the potholes comes with intent = new intent (intent. action_view, Uri); startactivity (intent) ;}} // Add a button to the home page. show (); // display }}

//Report.java
Package COM. demo. android. BMI; import Java. text. decimalformat; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textview; public class report extends activity {@ overrideprotected void oncreat E (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. report); findviews (); showresults (); setlistensers ();} private button button_back; private textview view_result; private textview view_suggest; private void findviews () {button_back = (button) findviewbyid (R. id. report_back); view_result = (textview) findviewbyid (R. id. result_r); view_su Ggest = (textview) findviewbyid (R. id. suggest_r);} // listen for button clicksprivate void setlistensers () {button_back.setonclicklistener (backmain);} private button. onclicklistener backmain = new button. onclicklistener () {public void onclick (view v) {// close this activityreport. this. finish () ;}}; private void showresults () {decimalformat NF = new decimalformat ("0.00"); bundle bunde = This. getintent (). Getextras (); // activity. getintent () can get the passed intent object entity // getextras () gets the bunde object double height = Double attached to the intent. parsedouble (bunde. getstring ("key_height")/100; double weight = double. parsedouble (bunde. getstring ("key_weight"); double BMI = weight/(height * Height); view_result.settext (getstring (R. string. bmi_result) + NF. format (BMI); // give health adviceif (BMI> 25) {shownotification (BMI); view_sugg EST. settext (R. string. advice_heavy);} else if (BMI <20) {view_suggest.settext (R. string. advice_light);} else {view_suggest.settext (R. string. advice_average) ;}} protected void shownotification (double BMI) {// todo auto-generated method stubicationicationmanager barmanager = (icationicationmanager) getsystemservice (icationication_service ); // obtain the built-in function notification barmsg = new notification (Android. r. drawable. Stat_sys_warning, "Welcome to the WEAP world! ", System. currenttimemillis (); // The pendingintent contentintent = pendingintent is displayed immediately. getactivity (this, 0, // when you click this information in the status bar, execute an activitynew intent (this, BMI. class), pendingintent. flag_update_current); // The third parameter is the most important. It defines the intentbarmsg to be called. setlatesteventinfo (report. this, "Your BMI value is too high", "inform the Ministry of Railways", // appears in the status bar drop-down detail notification in contentintent); barmanager. Y (0, barmsg); // transfer the information to the framework and display it in the status bar }}

V1.8.5.10.52

Package COM. demo. android. BMI; import java.net. urisyntaxexception; 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; impo RT android. widget. textview; import android. widget. toast; public class BMI extends activity {/** called when the activity is first created. * // @ override // rewrite the public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // call setcontentview (R. layout. main); // display on the screen // listen for Button Clicks // obtain the button component by using the findviewbyid method, which is forcibly converted to the button = (button) to maintain the same type as the description of the XML file) findviewbyid (R. id. Submit); button. setonclicklistener (calcbmi);} private onclicklistener calcbmi = new onclicklistener () {public void onclick (view v) {decimalformat NF = new decimalformat ("0.00 "); // define double-precision floating point number try {edittext fieldheight = (edittext) findviewbyid (R. id. height); // get the number of edittext fieldweight = (edittext) findviewbyid (R. id. weight); // double height = double. parsedouble (fieldheight. gettext (). tostring ()/100; doub Le Weight = double. parsedouble (fieldweight. gettext (). tostring (); double BMI = weight/(height * Height); textview result = (textview) findviewbyid (R. id. result); result. settext ("your body mass index BMI value:" + NF. format (BMI); // here I started to use "@ string/bmi_result", and then use R. string. no result is returned for the bmi_result. It is estimated that the result has a relationship with the + number: textview fieldsuggest = (textview) findviewbyid (R. id. suggest); // obtain the control if (BMI> 25) {fieldsuggest. settext (R. string. advice_hea Vy);} else if (BMI <20) {fieldsuggest. settext (R. string. advice_light);} else {fieldsuggest. settext (R. string. advice_average);} openoptionsdialog (); // call the relevant dialog box by the way after each calculation of the BMI display suggestion} catch (exception OBJ) {toast. maketext (BMI. this, "It's really proud. You have to fill both empty spaces! ", Toast. length_long ). show (); // automatically hide the bread, which is friendly to the prompt} private void openoptionsdialog () {New alertdialog. builder (BMI. this ). settitle (R. string. about_title) // set the title. setmessage (R. string. about_msg) // set the content. setpositivebutton (R. string. OK _label, new dialoginterface. onclicklistener () {public void onclick (dialoginterface, int I) {// function body is empty, click to close}) // Add confirmation button. setnegativebutton (R. string. homepage_label, new dialoginterface. onclicklistener () {public void onclick (dialoginterface, int I) {URI uri = Uri. parse (getstring (R. string. homepage_uri); // note: the URL here must contain http: //; otherwise, the focus of the potholes comes with intent = new intent (intent. action_view, Uri); startactivity (intent) ;}} // Add a button to the home page. show (); // display }};}

V1.8.4.21.11

Package COM. demo. android. BMI; import Java. text. decimalformat; // function: format a number into a specific string format import android. app. activity; // The Android program must contain the two 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; // main class public class BMI extends activity {/** called when the activity is first created. * // @ override // rewrite the public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // call setcontentview (R. layout. main); // display on the screen // listen for Button Clicks // obtain the button component by using the findviewbyid method, which is forcibly converted to the button = (button) to maintain the same type as the description of the XML file) findviewbyid (R. id. submit); button. setonclicklistener (calcbmi);} private onclicklistener calcbmi = new onclicklistener () {public void onclick (view v) {decimalformat NF = new decimalformat ("0.00 "); // define double-precision floating point edittext fieldheight = (edittext) findviewbyid (R. id. height); // get the number of 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 body mass index BMI value:" + NF. format (BMI); // here I started to use "@ string/bmi_result", and then use R. string. bmi_result, The result cannot be textview fieldsuggest = (textview) findviewbyid (R. id. suggest); // obtain the control 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 );}}};}

Main. xml source code

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/height"    /><EditText android:id="@+id/height"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:inputType="numberDecimal"    android:text=""    /><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/weight"    /><EditText android:id="@+id/weight"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:inputType="numberDecimal"    android:text=""    /><Button  android:id="@+id/submit"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/bmi_btn"    /><TextView android:id="@+id/result"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/bmi_result"    /><TextViewandroid:id="@+id/suggest"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text=""    /></LinearLayout>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.