|
| 1 | +package com.calculadora.calculadora; |
| 2 | + |
| 3 | +import androidx.appcompat.app.AppCompatActivity; |
| 4 | + |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.TextView; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | + |
| 12 | +public class MainActivity extends AppCompatActivity { |
| 13 | + |
| 14 | + Button BtnZero, BtnUm, BtnDois, BtnTres, BtnQuatro, |
| 15 | + BtnCinco, BtnSeis, BtnSete, BtnOito, BtnNove, |
| 16 | + BtnSoma, BtnSubtracao, BtnMultiplicacao, BtnDivisao, |
| 17 | + BtnPonto, BtnIgual, BtnLimpar, BtnApagar; |
| 18 | + |
| 19 | + TextView display1, display2, display3; |
| 20 | + |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { //criação da tela |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + setContentView(R.layout.activity_main); |
| 26 | + |
| 27 | + //declarando os botões |
| 28 | + |
| 29 | + display1 = (TextView) findViewById(R.id.mainDisplay1); |
| 30 | + display2 = (TextView) findViewById(R.id.mainDisplay2); |
| 31 | + display3 = (TextView) findViewById(R.id.mainDisplay3); |
| 32 | + |
| 33 | + |
| 34 | + BtnZero = findViewById(R.id.mainBtnZero); |
| 35 | + BtnUm = (Button) findViewById(R.id.mainBtnUm); |
| 36 | + BtnDois = (Button) findViewById(R.id.mainBtnDois); |
| 37 | + BtnTres = (Button) findViewById(R.id.mainBtnTres); |
| 38 | + BtnQuatro = (Button) findViewById(R.id.mainBtnQuatro); |
| 39 | + BtnCinco = (Button) findViewById(R.id.mainBtnCinco); |
| 40 | + BtnSeis = (Button) findViewById(R.id.mainBtnSeis); |
| 41 | + BtnSete = (Button) findViewById(R.id.mainBtnSete); |
| 42 | + BtnOito = (Button) findViewById(R.id.mainBtnOito); |
| 43 | + BtnNove = (Button) findViewById(R.id.mainBtnNove); |
| 44 | + |
| 45 | + BtnSoma = (Button) findViewById(R.id.mainBtnSoma); |
| 46 | + BtnSubtracao = (Button) findViewById(R.id.mainBtnSubtracao); |
| 47 | + BtnMultiplicacao = (Button) findViewById(R.id.mainBtnMultiplicacao); |
| 48 | + BtnDivisao = (Button) findViewById(R.id.mainBtnDivisao); |
| 49 | + |
| 50 | + BtnPonto = (Button) findViewById(R.id.mainBtnPonto); |
| 51 | + BtnIgual = (Button) findViewById(R.id.mainBtnIgual); |
| 52 | + |
| 53 | + BtnApagar = (Button) findViewById(R.id.mainBtnApagar); |
| 54 | + BtnLimpar = (Button) findViewById(R.id.mainBtnLimpar); |
| 55 | + } |
| 56 | + |
| 57 | + /*-instanciando o click do botão, se for diferente de falso, o botão precionado irá setar o display 1 ou 2(depende de qual display ja esta setado) |
| 58 | + e será mostrado no display o numero que foi clicado nesse caso do 0 até 9 -*/ |
| 59 | + |
| 60 | + |
| 61 | + boolean bool = false; |
| 62 | + public void BtnZero (View v){ |
| 63 | + if (!bool) { |
| 64 | + String strZero = display1.getText().toString(); |
| 65 | + display1.setText(strZero + "0"); |
| 66 | + } else { |
| 67 | + String strZero = display2.getText().toString(); |
| 68 | + display2.setText(strZero + "0"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void BtnUm (View v){ |
| 73 | + if (!bool) { |
| 74 | + String strUm = display1.getText().toString(); |
| 75 | + display1.setText(strUm + "1"); |
| 76 | + } else { |
| 77 | + String strUm = display2.getText().toString(); |
| 78 | + display2.setText(strUm + "1"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void BtnDois (View v){ |
| 83 | + if (!bool) { |
| 84 | + String strDois = display1.getText().toString(); |
| 85 | + display1.setText(strDois + "2"); |
| 86 | + } else { |
| 87 | + String strDois = display2.getText().toString(); |
| 88 | + display2.setText(strDois + "2"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void BtnTres (View v){ |
| 93 | + if (!bool) { |
| 94 | + String strTres = display1.getText().toString(); |
| 95 | + display1.setText(strTres + "3"); |
| 96 | + } else { |
| 97 | + String strTres = display2.getText().toString(); |
| 98 | + display2.setText(strTres + "3"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void BtnQuatro (View v){ |
| 103 | + if (!bool) { |
| 104 | + String strQuatro = display1.getText().toString(); |
| 105 | + display1.setText(strQuatro + "4"); |
| 106 | + } else { |
| 107 | + String strQuatro = display2.getText().toString(); |
| 108 | + display2.setText(strQuatro + "4"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void BtnCinco (View v){ |
| 113 | + if (!bool) { |
| 114 | + String strCinco = display1.getText().toString(); |
| 115 | + display1.setText(strCinco + "5"); |
| 116 | + } else { |
| 117 | + String strCinco = display2.getText().toString(); |
| 118 | + display2.setText(strCinco + "5"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public void BtnSeis (View v){ |
| 123 | + if (!bool) { |
| 124 | + String strSeis = display1.getText().toString(); |
| 125 | + display1.setText(strSeis + "6"); |
| 126 | + } else { |
| 127 | + String strSeis = display2.getText().toString(); |
| 128 | + display2.setText(strSeis + "6"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public void BtnSete (View v){ |
| 133 | + if (!bool) { |
| 134 | + String strSete = display1.getText().toString(); |
| 135 | + display1.setText(strSete + "7"); |
| 136 | + } else { |
| 137 | + String strSete = display2.getText().toString(); |
| 138 | + display2.setText(strSete + "7"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void BtnOito (View v){ |
| 143 | + if (!bool) { |
| 144 | + String strOito = display1.getText().toString(); |
| 145 | + display1.setText(strOito + "8"); |
| 146 | + } else { |
| 147 | + String strOito = display2.getText().toString(); |
| 148 | + display2.setText(strOito + "8"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public void BtnNove (View v){ |
| 153 | + if (!bool) { |
| 154 | + String strNove = display1.getText().toString(); |
| 155 | + display1.setText(strNove + "9"); |
| 156 | + } else { |
| 157 | + String strNove = display2.getText().toString(); |
| 158 | + display2.setText(strNove + "9"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /*-instanciando o click do botão, caso for verdadeiro o botão é chamado e exibe os operadores matematicos "+,-,*,/" no display3 -*/ |
| 163 | + |
| 164 | + public void BtnSoma (View v){ |
| 165 | + bool = true; |
| 166 | + display3.setText("+"); |
| 167 | + } |
| 168 | + |
| 169 | + public void BtnSubtracao (View v){ |
| 170 | + bool = true; |
| 171 | + display3.setText("-"); |
| 172 | + } |
| 173 | + |
| 174 | + public void BtnMultiplicacao (View v){ |
| 175 | + bool = true; |
| 176 | + display3.setText("x"); |
| 177 | + } |
| 178 | + |
| 179 | + public void BtnDivisao (View v){ |
| 180 | + bool = true; |
| 181 | + display3.setText("/"); |
| 182 | + } |
| 183 | + /*-instanciando o click do botão PONTO(.) chama a aplicação no display que estiver instanciado display 1 ou 2, |
| 184 | + deixando o usuario colcar somente 1 ponto em cada display e exibe uma mensagem na tela de que o ponto ja foi adicionado-*/ |
| 185 | + public void BtnPonto (View v){ |
| 186 | + if (!bool) { |
| 187 | + String strPonto = display1.getText().toString(); |
| 188 | + if (strPonto.contains(".")) { |
| 189 | + Toast.makeText(MainActivity.this, "Ja existe Ponto", Toast.LENGTH_LONG).show(); |
| 190 | + } else { |
| 191 | + String str = display1.getText().toString(); |
| 192 | + display1.setText(str + "."); |
| 193 | + } |
| 194 | + } else { |
| 195 | + String strPonto = display2.getText().toString(); |
| 196 | + if (strPonto.contains(".")) { |
| 197 | + Toast.makeText(MainActivity.this, "Ja existe Ponto", Toast.LENGTH_LONG).show(); |
| 198 | + } else { |
| 199 | + String str = display2.getText().toString(); |
| 200 | + display2.setText(str + "."); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +/*-faz a chamada do botão APAGAR no display que está selecionado, ele apaga a string uma a uma até mesmo o ponto adicionado,-*/ |
| 205 | + public void BtnApagar (View v){ |
| 206 | + if (!bool) { |
| 207 | + String str_display1 = display1.getText().toString(); |
| 208 | + |
| 209 | + if (str_display1.length() > 1) { |
| 210 | + String str_Apagar = str_display1.substring(0, str_display1.length() - 1); |
| 211 | + display1.setText(str_Apagar); |
| 212 | + } else { |
| 213 | + display1.setText(""); |
| 214 | + } |
| 215 | + } else { |
| 216 | + String str_display2 = display2.getText().toString(); |
| 217 | + if (str_display2.length() > 1) { |
| 218 | + String str_Apagar = str_display2.substring(0, str_display2.length() - 1); |
| 219 | + display2.setText(str_Apagar); |
| 220 | + } else { |
| 221 | + display2.setText(""); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +/*-chama o botão que limpa todas as telas displays 1,2 e 3-*/ |
| 226 | + public void BtnLimpar (View v){ |
| 227 | + bool = false; |
| 228 | + display1.setText(""); |
| 229 | + display2.setText(""); |
| 230 | + display3.setText(""); |
| 231 | + } |
| 232 | +/*- faz a chamada do botão igual que faz o calculo, capturando as strings dos displays 1 e 2 e transformando em inteiros, |
| 233 | +o display 3 e verificado qual operador foi selecionado, por exemplo, |
| 234 | +se foi selecionado o botão de + ele irá capturar os dados em string nos displays 1 e 2 |
| 235 | +e converte-los em inteiros, o resultado é exibido em forma de TOAST longo na tela -*/ |
| 236 | + public void BtnIgual (View v){ |
| 237 | + String str_display1 = display1.getText().toString(); |
| 238 | + String str_display2 = display2.getText().toString(); |
| 239 | + String str_display3 = display3.getText().toString(); |
| 240 | + |
| 241 | + if (display2.length() > 0) { |
| 242 | + int txt1 = Integer.parseInt(str_display1); |
| 243 | + int txt2 = Integer.parseInt(str_display2); |
| 244 | + if (str_display3.contains("+")) { |
| 245 | + int resultado = txt1 + txt2; |
| 246 | + String str = Integer.toString(resultado); |
| 247 | + Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); |
| 248 | + |
| 249 | + } else if (str_display3.contains("-")) { |
| 250 | + int resultado = txt1 - txt2; |
| 251 | + String str = Integer.toString(resultado); |
| 252 | + Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); |
| 253 | + |
| 254 | + } else if (str_display3.contains("x")) { |
| 255 | + int resultado = txt1 * txt2; |
| 256 | + String str = Integer.toString(resultado); |
| 257 | + Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); |
| 258 | + |
| 259 | + } else if (str_display3.contains("/")) { |
| 260 | + int resultado = txt1 / txt2; |
| 261 | + String str = Integer.toString(resultado); |
| 262 | + Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); |
| 263 | + } else { |
| 264 | + Toast.makeText(MainActivity.this, "Operação invádida.", Toast.LENGTH_SHORT).show(); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + |
0 commit comments