Você está na página 1de 8

6.

Calculadora Basica

Activity.Main
-----------------------------------------------------------------------------------
--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ingrese la expresión"
android:inputType="none"
android:textSize="20sp"
android:gravity="end"
android:layout_marginTop="8dp" />

<TextView
android:id="@+id/txt_resultado"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"
android:gravity="end"
android:layout_marginTop="8dp" />

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:padding="16dp">

<!-- Fila 1 -->


<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="C" />

<Button
android:id="@+id/btn_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="/" />

<Button
android:id="@+id/btn_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="*" />

<Button
android:id="@+id/btn_backspace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="←" />

<!-- Fila 2 -->


<Button
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="7" />

<Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="8" />

<Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="9" />

<Button
android:id="@+id/btn_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="-" />

<!-- Fila 3 -->


<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="4" />

<Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="5" />

<Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="6" />
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="+" />

<!-- Fila 4 -->


<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="1" />

<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="2" />

<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="3" />

<Button
android:id="@+id/btn_equals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="=" />

<!-- Fila 5 -->


<Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:text="0" />

<Button
android:id="@+id/btn_decimal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="." />

</GridLayout>
</LinearLayout>

MainActivity
-----------------------------------------------------------------------------------
------------------------------
package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText edtInput;


private TextView txtResultado;

private double numero1 = Double.NaN;


private double numero2;

private String operador = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edtInput = findViewById(R.id.edt_input);
txtResultado = findViewById(R.id.txt_resultado);

Button btnClear = findViewById(R.id.btn_clear);


Button btnDivide = findViewById(R.id.btn_divide);
Button btnMultiply = findViewById(R.id.btn_multiply);
Button btnBackspace = findViewById(R.id.btn_backspace);
Button btn7 = findViewById(R.id.btn_7);
Button btn8 = findViewById(R.id.btn_8);
Button btn9 = findViewById(R.id.btn_9);
Button btnSubtract = findViewById(R.id.btn_subtract);
Button btn4 = findViewById(R.id.btn_4);
Button btn5 = findViewById(R.id.btn_5);
Button btn6 = findViewById(R.id.btn_6);
Button btnAdd = findViewById(R.id.btn_add);
Button btn1 = findViewById(R.id.btn_1);
Button btn2 = findViewById(R.id.btn_2);
Button btn3 = findViewById(R.id.btn_3);
Button btnEquals = findViewById(R.id.btn_equals);
Button btn0 = findViewById(R.id.btn_0);
Button btnDecimal = findViewById(R.id.btn_decimal);

// Configurar los listeners de los botones


btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clear();
}
});

btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operador = "/";
performOperation();
}
});

btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operador = "*";
performOperation();
}
});

btnBackspace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backspace();
}
});

btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("7");
}
});

btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("8");
}
});

btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("9");
}
});

btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operador = "-";
performOperation();
}
});

btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("4");
}
});

btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("5");
}
});

btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("6");
}
});

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operador = "+";
performOperation();
}
});

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("1");
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("2");
}
});

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("3");
}
});

btnEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation();
operador = "";
}
});

btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput("0");
}
});

btnDecimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendToInput(".");
}
});
}

private void appendToInput(String str) {


edtInput.getText().append(str);
}

private void performOperation() {


if (!Double.isNaN(numero1)) {
String input = edtInput.getText().toString();
if (!input.isEmpty()) {
numero2 = Double.parseDouble(input);
double result = 0;

switch (operador) {
case "+":
result = numero1 + numero2;
break;
case "-":
result = numero1 - numero2;
break;
case "*":
result = numero1 * numero2;
break;
case "/":
if (numero2 != 0) {
result = numero1 / numero2;
} else {
txtResultado.setText("Error");
return;
}
break;
}

txtResultado.setText(String.valueOf(result));
numero1 = result;
}
} else {
numero1 = Double.parseDouble(edtInput.getText().toString());
}

edtInput.setText("");
}

private void clear() {


numero1 = Double.NaN;
numero2 = 0;
edtInput.setText("");
txtResultado.setText("0");
}

private void backspace() {


String currentInput = edtInput.getText().toString();
if (!currentInput.isEmpty()) {
currentInput = currentInput.substring(0, currentInput.length() - 1);
edtInput.setText(currentInput);
}
}
}

Você também pode gostar