Calculadora.java
Autor: Leomar | Publicado: 2015-03-07T09:31:00-08:00

package br.com.android.calculadora;

import java.text.NumberFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Calculadora extends Activity &nbsp;{
&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;
// Definição dos objetos e variáveis modulares que&nbsp;
// serão utilizados dentro da classe

&nbsp; &nbsp;EditText primeiro;
&nbsp; &nbsp;EditText segundo;
&nbsp; &nbsp;EditText resultado;
&nbsp; &nbsp;Button btnSomar;
&nbsp; &nbsp;Button btnSubtrair;
&nbsp; &nbsp;Button btnMultiplicar;
&nbsp; &nbsp;Button btnDividir;
&nbsp; &nbsp;Double result = (double)0;&nbsp;

&nbsp; &nbsp;// Chamada quando a activity é criada pela primeira vez
&nbsp; &nbsp;@Override
&nbsp; &nbsp;public void onCreate(Bundle savedInstanceState) {
&nbsp; &nbsp; &nbsp; &nbsp;super.onCreate(savedInstanceState);
&nbsp; &nbsp; &nbsp; &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; // Associa um arquivo de layout a esta classe
&nbsp; &nbsp; &nbsp; &nbsp; //
&nbsp; &nbsp; &nbsp; &nbsp;setContentView(R.layout.activity_calculadora);
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;try{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Instanciando os objetos de acordo com o layout XML&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// definido no arquivo main.xml
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;primeiro = (EditText)findViewById(R.id.edNumero1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;segundo = (EditText)findViewById(R.id.edNumero2);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultado = (EditText)findViewById(R.id.edResultado);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnSomar = (Button)findViewById(R.id.btnSomar);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnSubtrair = (Button)findViewById(R.id.btnSubtrair);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnMultiplicar = (Button)findViewById(R.id.btnMultiplicar);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnDividir = (Button)findViewById(R.id.btnDividir);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Associando o evento OnClickListener de cada um dos botões&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// a uma fução específica
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnSomar.setOnClickListener(onClick_btnSomar);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnSubtrair.setOnClickListener(onClick_btnSubtrair);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnMultiplicar.setOnClickListener(onClick_btnMultiplicar);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;btnDividir.setOnClickListener(onClick_btnDividir);
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Tratamento de erro&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem(e.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}

&nbsp; &nbsp;// Cria um anonymous implementation para OnClickListener&nbsp;
&nbsp; &nbsp;OnClickListener onClick_btnSomar = new OnClickListener() { &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = getDouble(primeiro.getText().toString()) + getDouble(segundo.getText().toString());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MostraResultado();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem("onClick_btnSomar");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Gravar no LogCat uma informação para auxiliar&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//no processo de desenvolvimento e correção de bugs
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.i("Somar", "clicou no botão Somar");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Gravar no LogCat o erro ocorrido
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TrataErro(e, "Botão Somar");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}; &nbsp; &nbsp;

&nbsp; &nbsp;OnClickListener onClick_btnSubtrair = new OnClickListener() { &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v){ &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = Double.valueOf(primeiro.getText().toString()) - Double.valueOf(segundo.getText().toString());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MostraResultado();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem("onClick_btnSubtrair");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TrataErro(e, "Botão Subtrair");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;};

&nbsp; &nbsp;OnClickListener onClick_btnMultiplicar = new OnClickListener() { &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) { &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = Double.valueOf(primeiro.getText().toString()) * Double.valueOf(segundo.getText().toString());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MostraResultado();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem("onClick_btnMultiplicar");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TrataErro(e, "Botão Multiplicar");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}; &nbsp; &nbsp;

&nbsp; &nbsp;OnClickListener onClick_btnDividir = new OnClickListener() { &nbsp; &nbsp;
&nbsp; &nbsp; public void onClick(View v) {&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = Double.valueOf(primeiro.getText().toString()) / Double.valueOf(segundo.getText().toString());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MostraResultado();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem("onClick_btnDividir");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TrataErro(e, "Botão Dividir");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}; &nbsp; &nbsp;
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp;//
&nbsp; &nbsp;// Desativando o botão voltar do aparelho
&nbsp; &nbsp;//
&nbsp; &nbsp;@Override
&nbsp; &nbsp;public void onBackPressed() {
&nbsp; &nbsp; &nbsp; &nbsp;return;
&nbsp; &nbsp;}

&nbsp; &nbsp;public void onClick_Sair(View v) {
&nbsp; &nbsp; &nbsp; &nbsp;try
&nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Criando uma caixa de diálogo para sair ou não do sistema
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AlertDialog.Builder builder = new AlertDialog.Builder(this); &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.setMessage("Você tem certeza que deseja sair?") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//informando o título da caixa de texto
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setTitle("Atenção:")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setIcon(android.R.drawable.ic_menu_info_details)&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//setCancelable = false
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//desabilita o botão Voltar com isso obrigando o usuário&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//a selecionar um dos botões do AlertDialog
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setCancelable(false) &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(DialogInterface dialog, int id) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Calculadora.this.finish();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.setNegativeButton("Não", new DialogInterface.OnClickListener() {&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(DialogInterface dialog, int id) {&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dialog.cancel(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}); &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AlertDialog alert = builder.create(); &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert.show(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mensagem(e.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}

&nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp;// Apresentar o resultado do cálculo no objeto "resultado"
&nbsp; &nbsp;// através do método setText
&nbsp; &nbsp;//
&nbsp; &nbsp;private void MostraResultado()
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;String strResult = NumberFormat.getInstance().format(result);
&nbsp; &nbsp; &nbsp; &nbsp;resultado.setText(strResult);
&nbsp; &nbsp;}

&nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp;// Converter um valor String para Double
&nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp;private Double getDouble(String pValue)
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;if (pValue == null || pValue.length() == 0 || pValue.equals("."))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Double.valueOf(0);
&nbsp; &nbsp; &nbsp; &nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Double.parseDouble(pValue);
&nbsp; &nbsp;}

&nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp;// Apresentar mensagem de erro ou informações oa usuário
&nbsp; &nbsp;//&nbsp;
&nbsp; &nbsp;private void Mensagem(String pMens)
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;Toast.makeText(this, pMens, Toast.LENGTH_SHORT).show();
&nbsp; &nbsp;}

&nbsp; &nbsp;private void TrataErro(Exception e, String pMens)
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;Mensagem(e.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp;Log.e(getString(R.string.app_name), pMens, e);
&nbsp; &nbsp;}
}