1
Friends, now the doubt is the following, I need the operation "+", be present to accomplish the sum, but always leave 1+2 and never the two make the sum. Does anyone have any idea what it might be ?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Inicio extends Activity {
TextView tela;
Button btnreset, btnraiz, btnporcentagem, btndesfazer,
btnsubtracao, btnum1, btnum2, btnum3,
btnsoma, btnum4, btnum5, btnum6,
btnmultiplicacao, btnum7, btnum8, btnum9,
btndivisao, btnum0, btnponto, btnresutado;
String resultado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
tela = (TextView) findViewById(R.id.tela);
btnreset = (Button) findViewById(R.id.reset);
btnraiz = (Button) findViewById(R.id.raiz);
btnporcentagem = (Button) findViewById(R.id.porcentagem);
btndesfazer = (Button) findViewById(R.id.desfazer);
btnsubtracao = (Button) findViewById(R.id.subtracao);
btnum1 = (Button) findViewById(R.id.um);
btnum2 = (Button) findViewById(R.id.dois);
btnum3 = (Button) findViewById(R.id.tres);
btnsoma = (Button) findViewById(R.id.soma);
btnum4 = (Button) findViewById(R.id.quatro);
btnum5 = (Button) findViewById(R.id.cinco);
btnum6 = (Button) findViewById(R.id.seis);
btnmultiplicacao = (Button) findViewById(R.id.multiplicacao);
btnum7 = (Button) findViewById(R.id.sete);
btnum8 = (Button) findViewById(R.id.oito);
btnum9 = (Button) findViewById(R.id.nove);
btndivisao = (Button) findViewById(R.id.divisao);
btnum0 = (Button) findViewById(R.id.zero);
btnponto = (Button) findViewById(R.id.ponto);
btnresutado = (Button) findViewById(R.id.resultado);
}
public void digito(View v) {
switch (v.getId()) {
case R.id.um:
resultado+=Integer.parseInt(btnum1.getText().toString());
tela.append(btnum1.getText());
break;
case R.id.dois:
resultado+=Integer.parseInt(btnum2.getText().toString());
tela.append(btnum2.getText());
break;
case R.id.soma:
resultado+=String.valueOf(btnsoma.getText().toString());
tela.append(btnsoma.getText());
break;
case R.id.resultado:
tela.append(resultado);
break;
}
}
}
Gave a little light to your idea, now I put the buttons in the case, picking by the Viewer ID . The problem now is in converting the data, as the result ta leaving null1+2. I edited the contents above.
– Diego Kappaun
The variable
resultado
It’s coming out because I don’t start it, in onCreate you start it as follows,resultado="";
– Skywalker
Yes, thanks for the help. Only the part of the result is missing the sum of the values correctly.
– Diego Kappaun
I created a second option of the calculation method, try it, it should work. SOURCE: http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form#3423360
– Skywalker