Você está na página 1de 8

Exercícios – Eclipse ADT

01 – Criar novo projeto Android: CalcularIMC

02 – Criar tela IMCActivity


03 – Coloque form widget do tipo Large Text:

Mude o texto para Peso:

04 – coloque widget Text Field / Number(decimal)

05 – Coloque form widget do tipo Large Text:

Mude o texto para Altura:

06 – coloque widget Text Field / Number(decimal)


07 – Coloque form widget do tipo Button:

Mude o texto para Calcular:

08 – Coloque form widget do tipo Large Text:

Mude o texto para IMC:

09 – Coloque form widget do tipo Large Text:

Mude o texto para Situação:

10 – Coloque form widget do tipo Large Text:

Mude o texto para “ “ (espaço em branco):


11 – Clique no widget Text Field ao lado do label Peso

Altere o id para peso

11 – Clique no widget Text Field ao lado do label Altura

Altere o id para altura


11 – Clique no widget button

Altere o id para btCalcular

12 – Clique no widget Text Field ao lado do texto IMC:

Altere o id para labelIMC


12 – Clique no widget Text Field ao lado do texto IMC:

Altere o id para labelSituacao

Parte java:

Mude a guia para o java: IMCActivity.java


Alterar o método

protected void onCreate(Bundle savedInstanceState)


para incluir as linhas de código:

Button b = (Button) findViewById(R.id.btCalcular);

b.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
EditText p = (EditText) findViewById(R.id.peso);
EditText a = (EditText) findViewById(R.id.altura);

double p1 = Double.parseDouble(p.getText().toString());
double a1 = Double.parseDouble(a.getText().toString());
double imc = p1 / (a1 * a1);

TextView i = (TextView) findViewById(R.id.labelIMC);


TextView s = (TextView)
findViewById(R.id.labelSituacao);

i.setText(" " + imc);


if (imc < 18.5){
s.setText("Abaixo do Peso");
}
else
if ((imc >= 18.5) && (imc < 25)) {
s.setText("No Peso NORMAL!!!");
}
else
if ((imc >= 25) && (imc < 30)) {
s.setText("Acima do Peso");
}
else
if ((imc > 30)) {
s.setText("Obeso");
}
}
});

Se apresentar erros, posicionar o mouse sobre o erro e ver as soluções.

Na maioria basta importar as classes necessárias.

Execute o app

Exemplo de saída esperada.

Você também pode gostar