Android development application instance: instance for calculating the standard weight (simple version)

Source: Internet
Author: User

 

The following is a simple example of calculating the standard weight. Select your own gender, enter your height, and click the Button to display your standard weight in Toast, check whether your weight meets the standards.

Method for Calculating the standard weight:

Male :( height cm-80) × 70 = standard weight female :( height cm-70) × 60 = standard weight

BMIActivity. java

1. package com. lingdududu. bmi;

2.

3. import java. text. DecimalFormat;

4. import java. text. NumberFormat;

5. import android. app. Activity;

6. import android. OS. Bundle;

7. import android. view. View;

8. import android. view. View. OnClickListener;

9. import android. widget. Button;

10. import android. widget. EditText;

11. import android. widget. RadioButton;

12. import android. widget. Toast;

13 ./*

14. * @ author lingdududu * the function of this program is to select your own Gender and enter your height. Then, click the button to display your standard weight in Toast.

15 .*/

16. public class BMIActivity extends Activity {

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

18. private Button countButton;

19. private EditText heighText;

20. private RadioButton maleBtn, femaleBtn;

21. String sex = "";

22. double height;

23. @ Override

24. public void onCreate (Bundle savedInstanceState ){

25. super. onCreate (savedInstanceState );

26. setContentView (R. layout. main );

27. // call the function for creating a view

28. creadView ();

29. // call the gender selection function

30. sexChoose ();

31. // call the Button to register the listener Function

32. setListener ();

33 .}

34.

35. // function for responding to the Button event

36. private void setListener (){

37. countButton. setOnClickListener (countListner );

38 .}

39.

40. private OnClickListener countListner = new OnClickListener (){

41.

42. @ Override

43. public void onClick (View v ){

44. // TODO Auto-generated method stub

45. Toast. makeText (BMIActivity. this, "you are a" + sexChoose () + "\ n"

46. + "your height is" + Double. parseDouble (heighText. getText (). toString () + "cm"

47. + "\ n your standard weight is" + getWeight (sexChoose (), height) + "kg", Toast. LENGTH_LONG)

48 .. show ();

49 .}

50 .};

51.

52. // gender selection function

53. private String sexChoose (){

54. if (maleBtn. isChecked ()){

55. sex = "male ";

56 .}

57. else if (femaleBtn. isChecked ()){

58. sex = "female ";

59 .}

60. return sex;

61 .}

62.

63. // create a function for the view

64. public void creadView (){

65. // txt((textview1_findviewbyid(r.id.txt );

66. countButton = (Button) findViewById (R. id. btn );

67. heighText = (EditText) findViewById (R. id. etx );

68. maleBtn = (RadioButton) findViewById (R. id. male );

69. femaleBtn = (RadioButton) findViewById (R. id. female );

70. // txt. setBackgroundResource (R. drawable. bg );

71 .}

72.

73. // formatted output function of the standard weight

74. private String format (double num ){

75. NumberFormat formatter = new DecimalFormat ("0.00 ");

76. String str = formatter. format (num );

77. return str;

78 .}

79.

80. // obtain the function of the standard weight.

81. private String getWeight (String sex, double height ){

82. height = Double. parseDouble (heighText. getText (). toString ());

83. String weight = "";

84. if (sex. equals ("male ")){

85. weight = format (height-80) * 0.7 );

86 .}

87. else {

88. weight = format (height-70) * 0.6 );

89 .}

90. return weight;

91 .}

92 .}

Main. xml

1. <? Xml version = "1.0" encoding = "UTF-8"?>

2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

3. android: orientation = "vertical"

4. android: layout_width = "fill_parent"

5. android: layout_height = "fill_parent"

6. android: background = "@ drawable/pic"

7.>

8. <TextView

9. android: id = "@ + id/txt"

10. android: layout_width = "fill_parent"

11. android: layout_height = "wrap_content"

12. android: gravity = "center"

13. android: text = "@ string/hello"

14. android: textSize = "16px"

15./>

16. <TextView

17. android: layout_width = "fill_parent"

18. android: layout_height = "wrap_content"

19. android: text = "@ string/sex"

20./>

21. <RadioGroup

22. android: layout_width = "fill_parent"

23. android: layout_height = "wrap_content"

24. android: orientation = "horizontal"

25.>

26. <RadioButton

27. android: id = "@ + id/male"

28. android: layout_width = "wrap_content"

29. android: layout_height = "wrap_content"

30. android: text = "male"

31./>

32. <RadioButton

33. android: id = "@ + id/female"

34. android: layout_width = "wrap_content"

35. android: layout_height = "wrap_content"

36. android: text = "female"

37./>

38. </RadioGroup>

39. <TextView

40. android: layout_width = "fill_parent"

41. android: layout_height = "26px"

42. android: text = "@ string/heigh"

43./>

44. <EditText

45. android: id = "@ + id/etx"

46. android: layout_width = "fill_parent"

47. android: layout_height = "wrap_content"

48./>

49. <Button

50. android: id = "@ + id/btn"

51. android: layout_width = "fill_parent"

52. android: layout_height = "wrap_content"

53. android: text = "@ string/count"

54./>

55. </LinearLayout>

:

 

This article is from the "IT" blog

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.