0
I’m a beginner and I’m developing a simple calculator in android studio, but it only shows the result of two values, ex: 1+1=2. I would like to do several operations at the same time, e.g. 1+2*3/4
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static android.R.attr.id;
public class MainActivity extends AppCompatActivity {
private double operador1;
private double operador2;
private double operadores;
boolean mAdicao, mSubtacao, mMultiplicacao, mDivisao;
private EditText txt_visor;
private Button n1;
private Button n2;
private Button n3;
private Button n4;
private Button n5;
private Button n6;
private Button n7;
private Button n8;
private Button n9;
private Button n0;
private Button dois_zeros;
private Button ponto;
private Button soma;
private Button subitracao;
private Button multiplicacao;
private Button divisao;
private Button porcento;
private Button potenciacao;
private Button raiz;
private Button igual;
private Button limpar;
private Button sair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
txt_visor = (EditText) findViewById(R.id.txt_visor);
n1 = (Button) findViewById(R.id.n1);
n2 = (Button) findViewById(R.id.n2);
n3 = (Button) findViewById(R.id.n3);
n4 = (Button) findViewById(R.id.n4);
n5 = (Button) findViewById(R.id.n5);
n6 = (Button) findViewById(R.id.n6);
n7 = (Button) findViewById(R.id.n7);
n8 = (Button) findViewById(R.id.n8);
n9 = (Button) findViewById(R.id.n9);
n0 = (Button) findViewById(R.id.n0);
dois_zeros = (Button) findViewById(R.id.dois_zeros);
ponto = (Button) findViewById(R.id.ponto);
soma = (Button) findViewById(R.id.soma);
subitracao = (Button) findViewById(R.id.subitracao);
multiplicacao = (Button) findViewById(R.id.multiplicacao);
divisao = (Button) findViewById(R.id.divisao);
porcento = (Button) findViewById(R.id.porcento);
potenciacao = (Button) findViewById(R.id.potenciacao);
raiz = (Button) findViewById(R.id.raiz);
igual = (Button) findViewById(R.id.igual);
limpar = (Button) findViewById(R.id.limpar);
/* setando o número de cada botão*/
n1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "1");
}
});
n2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "2");
}
});
n3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "3");
}
});
n4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "4");
}
});
n5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "5");
}
});
n6.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "6");
}
});
n7.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "7");
}
});
n8.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "8");
}
});
n9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "9");
}
});
n0.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "0");
}
});
dois_zeros.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt_visor.setText(txt_visor.getText() + "00");
}
});
soma.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (txt_visor==null){
txt_visor.setText("");
}else {
operador1 = Double.parseDouble(txt_visor.getText() + "");
mAdicao = true;
txt_visor.setText(null);
}
}
});
subitracao.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (txt_visor==null){
txt_visor.setText("");
}else {
operador1 = Double.parseDouble(txt_visor.getText() + "");
mSubtacao = true;
txt_visor.setText(null);
}
}
});
multiplicacao.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (txt_visor==null){
txt_visor.setText("");
}else {
operador1 = Double.parseDouble(txt_visor.getText() + "");
mMultiplicacao = true;
txt_visor.setText(null);
}
}
});
divisao.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (txt_visor==null){
txt_visor.setText("");
}else {
operador1 = Double.parseDouble(txt_visor.getText() + "");
mDivisao = true;
txt_visor.setText(null);
}
}
});
igual.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
operador2 = Double.parseDouble(txt_visor.getText().toString()+"" );
if (mAdicao==true){
txt_visor.setText(operador1 +operador2 +"");
}
if (mSubtacao ==true){
txt_visor.setText(operador1-operador2+"");
}
}
});
}
Just accumulate the results in a variable for each change of operation.
– user28595
A tip is to delegate the responsibility for arithmetic operations to a class. This makes it easier to do the treatments and accumulate results in numerical expressions, which is what the calculator does.
– user28595
@diegofm can give an example?
– Rodolfo Allesson