1
+ package com .example .calculateimc ;
2
+
3
+ import androidx .appcompat .app .AppCompatActivity ;
4
+
5
+ import android .content .res .ColorStateList ;
6
+ import android .graphics .PorterDuff ;
7
+ import android .os .Bundle ;
8
+ import android .view .View ;
9
+ import android .view .inputmethod .InputMethod ;
10
+ import android .view .inputmethod .InputMethodManager ;
11
+ import android .widget .Button ;
12
+ import android .widget .EditText ;
13
+ import android .widget .TextView ;
14
+ import android .widget .Toast ;
15
+
16
+ import com .example .calculateimc .statics .StaticInfo ;
17
+
18
+ public class MainActivity extends AppCompatActivity {
19
+
20
+ private EditText peso ;
21
+ private EditText altura ;
22
+ private Button calcular ;
23
+ private Button limpar ;
24
+ private Button resultadoCor ;
25
+ private TextView resultadoImc ;
26
+ private TextView resultadoClassificacao ;
27
+
28
+ @ Override
29
+ protected void onCreate (Bundle savedInstanceState ) {
30
+ super .onCreate (savedInstanceState );
31
+ setContentView (R .layout .activity_main );
32
+
33
+ initComponents ();
34
+ handleEvents ();
35
+ }
36
+
37
+ private void initComponents ()
38
+ {
39
+ try {
40
+ peso = findViewById (R .id .peso );
41
+ altura = findViewById (R .id .altura );
42
+ calcular = findViewById (R .id .calcBtn );
43
+ limpar = findViewById (R .id .cleanBtn );
44
+ resultadoCor = findViewById (R .id .btnResult );
45
+ resultadoImc = findViewById (R .id .imc );
46
+ resultadoClassificacao = findViewById (R .id .classificacao );
47
+ }
48
+ catch (Exception ex ){
49
+ System .out .println ("initComponents(): " + ex .getMessage ());
50
+ }
51
+ }
52
+
53
+ private void handleEvents ()
54
+ {
55
+ try {
56
+ handleCalculateBtn ();
57
+ handleCleanBtn ();
58
+ }
59
+ catch (Exception ex ) {
60
+ System .out .println ("handleEvents(): " + ex .getMessage ());
61
+ }
62
+ }
63
+
64
+ private void handleCalculateBtn ()
65
+ {
66
+ try {
67
+ cleanFields ();
68
+ calcular .setOnClickListener (new View .OnClickListener () {
69
+ @ Override
70
+ public void onClick (View view ) {
71
+ if (!testEditTextIsValid (peso ) || !testEditTextIsValid (altura ))
72
+ Toast .makeText (MainActivity .this , "É necessário preencher todos os campos." , Toast .LENGTH_LONG ).show ();
73
+ else
74
+ {
75
+ closeInputKeyboard (view );
76
+ String weightInput = peso .getText ().toString ();
77
+ String heightInput = altura .getText ().toString ();
78
+ double weight = Double .parseDouble (weightInput );
79
+ double height = Double .parseDouble (heightInput );
80
+
81
+ double result = (weight / Math .pow (height , 2 ));
82
+ resultadoImc .setText (String .format ("%.2f" , result ));
83
+ setLabelAndButtonColor (result );
84
+ }
85
+ }
86
+ });
87
+ }
88
+ catch (Exception ex ){
89
+ System .out .println ("handleCalculateBtn(): " + ex .getMessage ());
90
+ }
91
+ }
92
+
93
+ private void closeInputKeyboard (View view )
94
+ {
95
+ try {
96
+ InputMethodManager input = (InputMethodManager ) getSystemService (MainActivity .INPUT_METHOD_SERVICE );
97
+ input .hideSoftInputFromWindow (view .getWindowToken (), 0 );
98
+ }
99
+ catch (Exception ex ){
100
+ System .out .println ("closeInputKeyboard(): " + ex .getMessage ());
101
+ }
102
+ }
103
+
104
+ private void handleCleanBtn ()
105
+ {
106
+ try {
107
+ limpar .setOnClickListener (new View .OnClickListener () {
108
+ @ Override
109
+ public void onClick (View view ) {
110
+ cleanFields ();
111
+ closeInputKeyboard (view );
112
+ }
113
+ });
114
+ }
115
+ catch (Exception ex ){
116
+ System .out .println ("handleCleanBtn(): " + ex .getMessage ());
117
+ }
118
+ }
119
+
120
+ private void cleanFields ()
121
+ {
122
+ try {
123
+ peso .setText ("" );
124
+ altura .setText ("" );
125
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .white ));
126
+ resultadoImc .setText ("" );
127
+ resultadoClassificacao .setText ("" );
128
+ }
129
+ catch (Exception ex ){
130
+ System .out .println ("cleanFields(): " + ex .getMessage ());
131
+ }
132
+ }
133
+
134
+ private boolean testEditTextIsValid (EditText editText )
135
+ {
136
+ try {
137
+ String text = editText .getText ().toString ();
138
+ return !text .isEmpty ();
139
+ }
140
+ catch (Exception ex ){
141
+ System .out .println ("testEditTextDouble(): " + ex .getMessage ());
142
+ return false ;
143
+ }
144
+ }
145
+
146
+ private void setLabelAndButtonColor (double result )
147
+ {
148
+ try {
149
+ if (result < 16.0 )
150
+ {
151
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .magrezaIII ));
152
+ resultadoClassificacao .setText (StaticInfo .MagrezaIII );
153
+ }
154
+ else if (result >= 16.0 && result <= 16.9 )
155
+ {
156
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .magrezaII ));
157
+ resultadoClassificacao .setText (StaticInfo .MagrezaII );
158
+ }
159
+ else if (result > 16.9 && result <= 18.4 )
160
+ {
161
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .magrezaI ));
162
+ resultadoClassificacao .setText (StaticInfo .MagrezaI );
163
+ }
164
+ else if (result > 18.4 && result <= 24.9 )
165
+ {
166
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .eutrofia ));
167
+ resultadoClassificacao .setText (StaticInfo .Eutrofia );
168
+ }
169
+ else if (result > 24.9 && result <= 29.9 )
170
+ {
171
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .preObesidade ));
172
+ resultadoClassificacao .setText (StaticInfo .PreObesidade );
173
+ }
174
+ else if (result > 29.9 && result <= 34.9 )
175
+ {
176
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .moderada ));
177
+ resultadoClassificacao .setText (StaticInfo .ObesidadeModerada );
178
+ }
179
+ else if (result > 34.9 && result <= 39.9 )
180
+ {
181
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .severa ));
182
+ resultadoClassificacao .setText (StaticInfo .ObesidadeSevera );
183
+ }
184
+ else
185
+ {
186
+ resultadoCor .setBackgroundColor (getResources ().getColor (R .color .muitoSevera ));
187
+ resultadoClassificacao .setText (StaticInfo .ObesidadeMuitoSevera );
188
+ }
189
+ }
190
+ catch (Exception ex ){
191
+ System .out .println ("setLabelAndButtonColor(): " + ex .getMessage ());
192
+ }
193
+ }
194
+
195
+ }
0 commit comments