Get method values to have result in ANDROID calculator

Asked

Viewed 975 times

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;

   }
   }
   }

1 answer

3

You can dry up your code a lot more, come on.

the methods num1,num2...num9,subtrai, etc can become one in the following way. In your xml you must be calling the methods via the attribute android:onclick=numX, right? make all buttons call only one method, android:onclick=digito and in java will create a single method for all numeric buttons and operators.

private void digito(View v){
   //a view que está sendo passada por 
   //parametro nada mais é que o proprio botão que foi pressionado.
   //então devemos dizer que essa view é um botão da seguinte maneira
   Button btDigito = (Button)v;
   //recuperamos o valor do botão
   String valor = btDigito.getText().toString();
   //adiciona o valor para um string que vai acumulando a formula
   result += valor;
}

Option 1

private void calcular(){
     Binding binding   = new Binding();  
     GroovyShell shell = new GroovyShell(binding);  
     //exibe na tela, não sei como você vai mostrar na tela o resultado
     // portanto defini esse método exibirResultado;
     exibirResultado(shell.evaluate(resultado));
}

Option 2

private void calcular(){
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    exibirResultado(engine.eval(resultado));
}

I confess that I did not test the method calculate only removed from researches I did in google, it is necessary to test, I believe that there are several ways to solve your problem, I just passed the simplest answer that I believe is, but if it doesn’t work out, let me know that I can reformulate the method in other ways.

  • 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.

  • The variable resultado It’s coming out because I don’t start it, in onCreate you start it as follows, resultado="";

  • Yes, thanks for the help. Only the part of the result is missing the sum of the values correctly.

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.