计算器在一个edittext上显示运算符号 - Android

时间:2014-03-28 03:39:57

标签: android calculator

所以我有一个简单的计算器,它可以工作,但它没有在edittext上显示完整的等式(如操作符号)。我现在要做的是在edittext中显示完整的等式。例如,我想显示8 + 8,然后当我点击=符号时,结果变为16时。我只想显示整个等式。我怎么能用我的代码呢?有人可以帮助我。

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);
   }

}

3 个答案:

答案 0 :(得分:0)

试试这个

 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

}

答案 1 :(得分:0)

更改您的calulate功能如下:

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);
   }

答案 2 :(得分:0)

最简单的方法是保留尽可能多的现有当前代码,只需创建第二个字符串来保存要显示的文本。所以你的insert()将成为:

 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);
}

您的每个操作方法都需要附加到displayStr。所以减号的那个将成为:

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

你的重置需要一个额外的行来清空显示字符串:

    displayStr = "";                     // Added

可能会有更多调整,但这应该让你朝着正确的方向前进。