2012-02-14 7 views
0

私は電卓をプログラミングしており、現在ほとんど完了しています。等号ボタンをプログラムします。ボタンの値がtrueの場合にテストします。 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

オペレータ "+"を置く場所の文字列をチェックするのはなぜですか? –

+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 –

+0

私はまだかなり理解していません。 – recheej

答えて

0

を押すごとにボタンを保存するために、スタックのデータ構造を使用します。私は、ボタンがクリックされたと別のボタンが、その前にクリックされたに等しい場合、いくつかのコードを実行し、これは私が持っているすべてでテストできるようにしたいです。 有効なシーケンス(operand operator operand equal、1 + 1 =)を検出すると、それを実行してスタックから値を削除します。

関連する問題