Last active
February 22, 2020 15:45
-
-
Save efturtle/ac31a870bb1096e094d1d3192e1176a0 to your computer and use it in GitHub Desktop.
code for the basic layout of https://gist.github.com/MEXSlacker/47f30e12d23d74c955b04e4616e76958 this layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.calculadoramodo1; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import org.w3c.dom.Text; | |
| public class MainActivity extends AppCompatActivity { | |
| //atributes | |
| private TextView campo; | |
| //variables para operar | |
| public double a = 0; | |
| public double b = 0; | |
| //variable que almacena cadena para numeros | |
| String cadena = ""; | |
| //muestra resultados anteriores | |
| String historial = ""; | |
| boolean bandera = false; | |
| //int para un switch, case of, we do an operation. | |
| int signo = 0; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| } | |
| @Override | |
| protected void onResume (){ | |
| super.onResume(); | |
| Toast.makeText(this, "Bienvenido de vuelta", Toast.LENGTH_SHORT).show(); | |
| } | |
| public void button5(View view){ | |
| TextView campo = findViewById(R.id.textView); //Toma el campo | |
| cadena += "5"; //le agrega un 5 a la cadena | |
| campo.setText(cadena); //para el usuario | |
| if(bandera == false){ //si no se a registrado la primera variable se llena | |
| a = Double.parseDouble(cadena); | |
| }else { //en caso que a ya este llenado | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button6 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "6"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button1 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "1"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button2 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "2"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button3 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "3"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button4 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "4"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button7 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "7"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button8 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "8"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button9 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "9"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| public void button0 (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| cadena += "0"; | |
| campo.setText(cadena); | |
| if(bandera == false){ | |
| a = Double.parseDouble(cadena); | |
| }else { | |
| b = Double.parseDouble(cadena); | |
| } | |
| } | |
| //botones de operaciones, llamados por el metodo escrito en el xml tag | |
| public void suma(View view){ | |
| TextView campo = findViewById(R.id.textView); //textview tomara valor del signo correspondiente | |
| campo.setText("+"); //igualamos el text a el signo | |
| signo = 1; //switch value for operation | |
| bandera = true; //bandera prendida para llenar valor b | |
| cadena = ""; //limpiamos el valor cadena para que no se acumulen los numeros ya puestos./ | |
| } | |
| public void resta (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| campo.setText("-"); | |
| signo = 2; | |
| cadena = ""; | |
| bandera = true; | |
| } | |
| public void multiplicacion (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| campo.setText("x"); | |
| signo = 3; | |
| cadena = ""; | |
| bandera = true; | |
| } | |
| public void division(View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| campo.setText("/"); | |
| signo = 4; | |
| cadena = ""; | |
| bandera = true; | |
| } | |
| public void enter (View view){ | |
| TextView campo = findViewById(R.id.textView); | |
| double temp = 0; | |
| switch (signo){ //what ever is the signo is the operation that the user decided to make. | |
| case 1: | |
| temp = a+b; //operation | |
| campo.setText(String.valueOf(temp)); | |
| bandera = false; //now a will fill in | |
| break; | |
| case 2: | |
| temp = a-b; | |
| campo.setText(String.valueOf(temp)); | |
| bandera = false; | |
| break; | |
| case 3: | |
| temp = a*b; | |
| campo.setText(String.valueOf(temp)); | |
| bandera = false; | |
| break; | |
| case 4: | |
| if (a!=0 && b!=0) { | |
| temp = a / b; | |
| campo.setText(String.valueOf(temp)); | |
| bandera = false; | |
| }else{ | |
| campo.setText("0"); | |
| Toast.makeText(this, "Division con 0", Toast.LENGTH_SHORT).show(); | |
| } | |
| break; | |
| } | |
| signo = 0; | |
| a=0; | |
| b=0; | |
| cadena = ""; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment