2012-02-14 35 views
0

我正在編程計算器,我目前幾乎完成;編寫等號按鈕。我希望能夠以測試是否被點擊等號按鈕,另一個按鈕是以前被點擊,執行一些代碼,這是我的全部:測試按鈕的值是否爲真。 Android項目

package rechee.cool; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class HelloAndroidActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 

    double counter1=0; 
    double counter2=0; 
    String theOperator; 
    //Just have two buttons so far, I'm going to have like 10 more 

    public EditText display;   

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Associate the button variable with the xml reference 

     display = (EditText) findViewById(R.id.editText1);} 

     //When button is clicked, display the text. How do I do this for the rest of my variables? 

     public void onClick(View v) 
     { 
      switch(v.getId()) 
      { 
       case R.id.bOne: 
       display.append("1"); 
        break; 
       case R.id.bTwo:      
        display.append("2"); 
        break; 
       case R.id.bThree: 
        display.append("3"); 
        break; 
       case R.id.bFour: 
        display.append("4"); 
        break; 
       case R.id.bFive: 
        display.append("5"); 
        break; 
       case R.id.bSix: 
        display.append("6"); 
        break; 

       case R.id.bSeven: 
        display.append("7"); 
        break; 
       case R.id.bEight: 
        display.append("8"); 
        break; 
       case R.id.bNine: 
        display.append("9"); 
        break; 
       case R.id.bZero: 
        display.append("0"); 
        break; 
       case R.id.bPoint: 
        display.append("."); 
        break; 
       case R.id.bClear: 
        display.setText(""); 
        break; 
       case R.id.bAdd: 
        // to get string of EditText 
        String display1= display.getText().toString(); 
        double displayValue= Double.parseDouble(display1); 
        //to test if display1 is double 
        counter1+= displayValue; 
        String theOperator = "+"; 
        break; 
       case R.id.bEqual: 
        if (R.id.bAdd=true){ 

        }   
      }      
     } 
    } 
} 
+0

爲什麼你不檢查運算符字符串,你把運算符「+」? – 2012-02-14 06:00:51

+0

你應該拿一個類變量來存儲哪個運算符(+, - ,*,/)被點擊過。當用戶點擊=按鈕時,你只需要檢查那個會告訴你哪個操作員被點擊的變量。這裏有一些示例項目可以幫助你http://technicalmumbojumbo.wordpress.com/2011/11/18/android-tutorial-part-calculator-app-application-example/和http://www.androiddom.com/2011 /04/creating-android-calculator-tutorial.html – 2012-02-14 06:01:17

+0

我還是不太明白。 – recheej 2012-02-14 06:43:20

回答

0

使用堆棧的數據結構來存儲每次按下該按鈕。 當您檢測到有效的序列(即操作數運算符等於,1 + 1 =)時,執行它並從堆棧中刪除這些值。