Question

So I have a simple calculator, it works but it doesn't show the full equation (like numbers with operators) on the edittext. What I want to do now is to show the full equation in the edittext. For example, I want to show 8 + 8 , then when I click the = sign, that's when the results change to 16. I just want to show the whole equation. How can I do that with my code? Can someone help me.

public class MainActivity extends Activity {

public String str ="";
 Character op = 'q';
 int i,num,numtemp;
    EditText showResult;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        showResult = (EditText)findViewById(R.id.result_id);


    }
      public void btn1Clicked(View v){
       insert(1);

      }

 public void btn2Clicked(View v){
       insert(2);

      }
      public void btn3Clicked(View v){
       insert(3);

      }
      public void btn4Clicked(View v){
       insert(4);

      }
      public void btn5Clicked(View v){
       insert(5);

      }
      public void btn6Clicked(View v){
       insert(6);
      }
      public void btn7Clicked(View v){
       insert(7);

      }
      public void btn8Clicked(View v){
       insert(8);

      }
      public void btn9Clicked(View v){
       insert(9);

      }

      public void btn0Clicked(View v){
           insert(0);

          }


      public void btnplusClicked(View v){
       perform();
       op = '+';

      }

 public void btnminusClicked(View v){
    perform();
         op = '-';

      }
      public void btndivideClicked(View v){
       perform();
       op = '/';

      }
      public void btnmultiClicked(View v){
       perform();
       op = '*';

      }
      public void btnequalClicked(View v){
       calculate();

      }

 public void btnclearClicked(View v){
       reset();
      }

 private void reset() {
  // TODO Auto-generated method stub
   str ="";
   op ='q';
   num = 0;
   numtemp = 0;
   showResult.setText("");
 }
 private void insert(int j) {
    // TODO Auto-generated method stub
       str = str+Integer.toString(j);
       num = Integer.valueOf(str).intValue();
       showResult.setText(str);


   }
      private void perform() {
    // TODO Auto-generated method stub
    str = "";
    numtemp = num;
   }
      private void calculate() {
    // TODO Auto-generated method stub
    if(op == '+')
     num = numtemp+num;
    else if(op == '-')
     num = numtemp-num;
    else if(op == '/')
     num = numtemp/num;
    else if(op == '*')
     num = numtemp*num;
    showResult.setText(""+num);
   }

}
Était-ce utile?

La solution

The easiest way, keeping as much of your existing current code as possible, is to just create a second string to hold the text to be displayed. So your insert() would become:

 private void insert(int j) {
    // TODO Auto-generated method stub
    str = str+Integer.toString(j);
    num = Integer.valueOf(str).intValue();
    displayStr += Integer.toString(j);   // Added
    showResult.setText(displayStr);
}

Each of your operation methods would need to append to the displayStr. So the one for the minus key would become:

public void btnminusClicked(View v){
    perform();
    op = '-';
    displayStr += "-";                   // Added     
}

And your reset would need an added line to empty-out the display string:

    displayStr = "";                     // Added

There may be a couple more tweaks, but that should get you going in the right direction.

Autres conseils

Change your calulate function as below:

private void calculate() {
    // TODO Auto-generated method stub
          String first=String.valueOf(numtemp);
          String second=String.valueOf(num);
    if(op == '+')
     num = numtemp+num;
    else if(op == '-')
     num = numtemp-num;
    else if(op == '/')
     num = numtemp/num;
    else if(op == '*')
     num = numtemp*num;

    showResult.setText(first+op+second+"="+num);
   }

try this

 private void calculate() {
// TODO Auto-generated method stub
if(op == '+')
 num = numtemp+num;
else if(op == '-')
 num = numtemp-num;
else if(op == '/')
 num = numtemp/num;
else if(op == '*')
 num = numtemp*num;
showResult.setText(""+numtemp+op+num);   //there is a change

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top