2013-03-20 37 views
1

我是Android的初學者。我想用一個輸入編輯文本來製作一個計算器。 當我點擊+按鈕它不會給出總和輸出。爲了得到正確的答案,我必須在兩個條目後點擊+按鈕。想找一個和我會做它作爲1「+」 1「+」「=。然後它會給2.這裏是我的代碼,someoneplease幫助我。帶一個輸入edittext的計算器android

public void onClick(View v){ 
    double sum=0; 
    switch(v.getId()){ 
    case R.id.buttonplus: 
     sum += Double.parseDouble(String.valueOf(textView.getText())); 
     numberDisplayed.delete(0,numberDisplayed.length()); 
     break; 
    case R.id.buttonequal: 
     resultView.setText(String.valueOf(sum)); 
     sum=0; 
    } 
+0

歡迎SO。你有什麼問題..? – Pragnani 2013-03-20 19:16:35

+0

我想製作一個計算器,但按鈕無法正常工作。我上面的代碼顯示了兩個加號和相等的情況。所以兩個添加兩個數字我不得不按兩個數字,我想添加然後它給了我一個ans按pluss按鈕。但我只需點擊一下鼠標即可獲得所有按鈕。 – shahwarcoder 2013-03-20 20:08:17

回答

1

如果我理解正確的話,你想總和告訴你按「等於」 button後。如果是這樣,那麼你需要有

sum += Double.parseDouble(String.valueOf(textView.getText())); 
在此行

case R.id.buttonequal: 
    sum += Double.parseDouble(String.valueOf(textView.getText())); 
    resultView.setText(String.valueOf(sum)); 
    sum=0; 

,不進入第二個數字然而,當您按下「加號」button所以總數只是第一個數字。然後,你必須再次按下它添加到sum

所以如果等於BTN壓,像

if (lastOp.equals("sub") 
{ 
    sum -= Double.parseDouble(String.valueOf(textView.getText())); 
... 
} 

public class SimpleCalculatorActivity extends Activity 
{ 
    //variables needing class scope 
    double answer = 0, number1, number2; 
    int operator = 0, number; 
    boolean hasChanged = false, flag = false; 
    String display = null; 
    String display2 = null; 
    String curDisplay = null; 
    String calcString = ""; 
    String inputLabel; 
    String inputString = null; 
    String inputString2 = null; 
    String inputString3 = null; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.setTitle("Super Duper Calculator"); 

     initButtons();  
    } 
     //when button is pressed, send num to calc function 
     button1.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        inputString = button1.getText().toString(); 
        displayCalc(inputString); 
       } 
      } 
     ); 

     button2.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        inputString = button2.getText().toString(); 
        displayCalc(inputString); 
       } 
      } 
     ); 
     ... 
     //send operator to calc function 
     addButton.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        calculation(1); 
       } 
      } 
     ); 

     subButton.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        calculation(2); 
       } 
      } 
     );  
     calcButton.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        calculation(5); 
       } 
      } 
     ); 

     clearButton.setOnClickListener 
     (new Button.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        calculation(6); 
       } 
      } 
     ); 
    } 

    //function to calculate 
    public void calculation(int input) 
    { 
     number = input; 

     //see which operator was clicked 
     switch (number) 
     { 
      case 1: 
       operator = 1; 
       hasChanged = true; 
       display = ""; 
       showDisplay("+"); 
       break; 
      case 2: 
       operator = 2; 
       hasChanged = true; 
       display = ""; 
       showDisplay("-");    
       break; 
      case 3: 
       operator = 3; 
       hasChanged = true; 
       display = ""; 
       showDisplay("*"); 
       break; 
      case 4: 
       operator = 4; 
       hasChanged = true; 
       display = ""; 
       showDisplay("/"); 
       break; 
      case 5: 
       number2 = Double.parseDouble(display2); 
       if(number2 == 0) 
       { 
        custErrMsg(); 
       } 
       else 
       { 
        operator(); 
        displayAnswer(answer); 
        hasChanged = true; 
       } 
       break; 
      case 6: 
       clear(); 
       break; 
      default: 
       clear(); 
       break; 
     } 
    } 

    private void operator() 
    { 
     if (operator != 0) 
     { 
      if (operator == 1) 
      { 
       answer = number1 + number2; 
      } 
      else if (operator == 2) 
      { 
       answer = number1 - number2; 
      } 
      else if (operator == 3) 
      { 
       answer = number1 * number2; 
      } 
      else if (operator == 4) 
      { 
       answer = number1/(number2); 
      } 
     } 
    } 

    private void displayCalc(String curValue) 
    { 
     String curNum = curValue; 

     if (!hasChanged) 
     { 
      if (display == null) 
      { 
       //display number if reset 
       inputString2 = curNum; 
       display = inputString2;  
       showDisplay(display); 

      } 
      else 
      { 
       //display previous input + new input 
       inputString2 = inputString2 + curNum; 
       display = display + curNum; 
       showDisplay(display); 
      } 
     } 
     else              
     { 
      displayNum2(curNum); 
     } 
    } 

    private void displayNum2 (String curValue2) 
    { 
     String curNum2; 
     curNum2 = curValue2; 
     if (!flag) 
     { 
      //display number if reset 
      inputString3 = curNum2; 
      display2 = inputString3; 
      number1 = Double.parseDouble(inputString2); 
      flag = true; 
     } 
     else 
     { 
      //display previous input + new input 
      inputString3 = curNum2; 
      display2 = display2 + curNum2; 
     } 
     showDisplay(inputString3); 
    } 

    private void displayAnswer(double curAnswer) 
    { 
     String finAnswer = String.valueOf(curAnswer); 
     TextView textView1 = (TextView) findViewById(R.id.textView1); 
     textView1.setBackgroundColor(0xffffffff); 
     textView1.setText(finAnswer); 
    } 

    private void showDisplay(String output) 
    { 
     inputLabel = output; 
     TextView textView1 = (TextView) findViewById(R.id.textView1); 
     textView1.setBackgroundColor(0xffffffff); 

     if (operator != 0) 
     { 
      curDisplay = textView1.getText().toString(); 
      textView1.setText(curDisplay + inputLabel); 
     }  
     else 
     { 
      textView1.setText(inputLabel); 
     } 
    } 
+0

但如果我想爲每個按鈕像減法按鈕,multiplybutton,除按鈕然後我應該做的。 – shahwarcoder 2013-03-20 19:44:32

+0

有不同的處理方法。當我回家時,我必須看看我很久以前是如何做的,但我想我會跟蹤上次使用哪個操作符,並將其傳遞給「equals」按鈕' – codeMagic 2013-03-20 19:49:53

+0

,但如果按照您的說法操作它會進入一個新的情況,總和的值將再次變爲0. – shahwarcoder 2013-03-20 19:52:50