방치하기

Android 계산기 tableLayout 본문

카테고리 없음

Android 계산기 tableLayout

Yi Junho 2010. 12. 31. 13:18
반응형















안드로이드 계산기 만들기
저걸 시켜서 과제로 만든건데 잘되는지는 모르겠다 ㅎ
package com.junho.cal;

import android.R.color;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

    int i;
 
    StringBuffer num1 = new StringBuffer();
    StringBuffer num2 = new StringBuffer();
    EditText result;
    int operationMode=0;
    boolean isFirstNumber = true; 
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Edit Text
        result = (EditText)findViewById(R.id.EditText01);
        result.setBackgroundColor(Color.BLACK);
        result.setTextColor(Color.WHITE);
        result.setTextSize(40);
        //버튼
       Button[] digit =new Button[10];
       

      	  digit[0] = (Button)findViewById(R.id.d0);
          Drawable alpha  = digit[0].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[1] = (Button)findViewById(R.id.d1);
          alpha  = digit[1].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[2] = (Button)findViewById(R.id.d2);
          alpha  = digit[2].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[3] = (Button)findViewById(R.id.d3);
          alpha  = digit[3].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[4] = (Button)findViewById(R.id.d4);
          alpha  = digit[4].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[5] = (Button)findViewById(R.id.d5);
          alpha  = digit[5].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[6] = (Button)findViewById(R.id.d6);
          alpha  = digit[6].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[7] = (Button)findViewById(R.id.d7);
          alpha  = digit[7].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[8] = (Button)findViewById(R.id.d8);
          alpha  = digit[8].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  digit[9] = (Button)findViewById(R.id.d9);
          alpha  = digit[9].getBackground();
      	  alpha.setAlpha(0); //알파값 0
      	  
      	  
      	  
      	  for(i=0; i < 10; i++)
      	  {
      		  digit[i].setOnClickListener(new Button.OnClickListener() {
      			  public void onClick(View v) {
      				  if(isFirstNumber)
      				  {
      					  
	      				   if (v.getId() == R.id.d0) {  
	      					 result.append(Integer.toString(0));
	      					 num1.append(Integer.toString(0));
	      				   }
	      				   
	      				   else if (v.getId() == R.id.d1) {  
	        					 result.append(Integer.toString(1));
	        					 num1.append(Integer.toString(1));
	        			   }
	      				   
	      				   else if (v.getId() == R.id.d2) {  
	        					 result.append(Integer.toString(2));
	        					 num1.append(Integer.toString(2));
	        				   }
	      				   else if (v.getId() == R.id.d3) {  
	        					 result.append(Integer.toString(3));
	        					 num1.append(Integer.toString(3));
	        				   }
	      				   else if (v.getId() == R.id.d4) {  
	        					 result.append(Integer.toString(4));
	        					 num1.append(Integer.toString(4));
	        				   }
	      				   else if (v.getId() == R.id.d5) {  
	        					 result.append(Integer.toString(5));
	        					 num1.append(Integer.toString(5));
	        				   }
	      				   else  if (v.getId() == R.id.d6) {  
	        					 result.append(Integer.toString(6));
	        					 num1.append(Integer.toString(6));
	        				   }
	      				   else  if (v.getId() == R.id.d7) {  
	      					 result.append(Integer.toString(7));
	      					num1.append(Integer.toString(7));
	      				   }
	      				   else  if (v.getId() == R.id.d8) {  
	      					 result.append(Integer.toString(8));
	      					num1.append(Integer.toString(8));
	      				   }
	      				   else  if (v.getId() == R.id.d9) {  
	      					 result.append(Integer.toString(9));
	      					num1.append(Integer.toString(9));
	      				   }
      				  }
      				  
      				  else
      				  {
	      				   if (v.getId() == R.id.d0) {  
		      					 result.append(Integer.toString(0));
		      					 num2.append(Integer.toString(0));
		      				   }
		      				   
		      				   else if (v.getId() == R.id.d1) {  
		        					 result.append(Integer.toString(1));
		        					 num2.append(Integer.toString(1));
		        			   }
		      				   
		      				   else if (v.getId() == R.id.d2) {  
		        					 result.append(Integer.toString(2));
		        					 num2.append(Integer.toString(2));
		        				   }
		      				   else if (v.getId() == R.id.d3) {  
		        					 result.append(Integer.toString(3));
		        					 num2.append(Integer.toString(3));
		        				   }
		      				   else if (v.getId() == R.id.d4) {  
		        					 result.append(Integer.toString(4));
		        					 num2.append(Integer.toString(4));
		        				   }
		      				   else if (v.getId() == R.id.d5) {  
		        					 result.append(Integer.toString(5));
		        					 num2.append(Integer.toString(5));
		        				   }
		      				   else  if (v.getId() == R.id.d6) {  
		        					 result.append(Integer.toString(6));
		        					 num2.append(Integer.toString(6));
		        				   }
		      				   else  if (v.getId() == R.id.d7) {  
			      					result.append(Integer.toString(7));
			      					num2.append(Integer.toString(7));
		      				   }
		      				   else  if (v.getId() == R.id.d8) {  
		      					   	result.append(Integer.toString(8));
			      					num2.append(Integer.toString(8));
		      				   }
		      				   else  if (v.getId() == R.id.d9) {  
			      					result.append(Integer.toString(9));
			      					num2.append(Integer.toString(9));
		      				   }
      				  }
				}
               });
      	  }
  	  	
	        Button clearBtn=(Button)findViewById(R.id.clear);
	        
	        clearBtn.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.clear)
					  {
						  result.setText(null);
						  num1 = new StringBuffer();
						  num2 = new StringBuffer();
						  isFirstNumber = true;
					  }
						  
					
				}
	        
	        });
	        Button plusBtn=(Button)findViewById(R.id.plus);
	        plusBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.plus)
					  {
						  if(isFirstNumber)
						  {
							  isFirstNumber = false;
							  operationMode = 1;
							  result.setText(null);
						  }
						  
					  }
				}
	        });
	        Button minusBtn=(Button)findViewById(R.id.minus);
	        minusBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.minus)
					  {
						  if(isFirstNumber)
						  {
							  result.setText(null);
							  isFirstNumber = false;
							  operationMode = 2;
						  }
					  }
				}
	        });
	        Button multiBtn=(Button)findViewById(R.id.multi);
	        multiBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.multi)
					  {
						  if(isFirstNumber)
						  {
							  result.setText(null);
							  isFirstNumber = false;
							  operationMode = 3;
						  }
					  }
				}
	        });
	        Button divBtn=(Button)findViewById(R.id.div);
	        divBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.div)
					  {
						  if(isFirstNumber)
						  {
							  result.setText(null);
							  isFirstNumber = false;
							  operationMode = 4;
						  }
					  }
				}
	        });
	        Button equalBtn=(Button)findViewById(R.id.equal);
	        equalBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.equal)
					  {
						  	String tempNum1=num1.toString();
				  			String tempNum2=num2.toString();
				  			float tempResult;
						  	switch (operationMode)
						  	{
							
						  		case 0:
						  			break;
						  		case 1:

						  			tempResult=Float.parseFloat(tempNum1)+Float.parseFloat(tempNum2);
						  			result.setText(Float.toString(tempResult));
						  			num1=new StringBuffer();
						  			num2=new StringBuffer();
						  			num1.append(Float.toString(tempResult));
						  			operationMode=0;
						  			isFirstNumber=true;
						  			break;
						  		case 2:
						  			tempResult=Float.parseFloat(tempNum1)-Float.parseFloat(tempNum2);
						  			result.setText(Float.toString(tempResult));
						  			num1=new StringBuffer();
						  			num2=new StringBuffer();
						  			num1.append(Float.toString(tempResult));
						  			operationMode=0;
						  			isFirstNumber=true;
						  			break;
						  			
						  		case 3:
						  			tempResult=Float.parseFloat(tempNum1)*Float.parseFloat(tempNum2);
						  			result.setText(Float.toString(tempResult));
						  			num1=new StringBuffer();
						  			num2=new StringBuffer();
						  			num1.append(Float.toString(tempResult));
						  			operationMode=0;
						  			isFirstNumber=true;
						  			break;
						  		
						  		case 4:
						  			tempResult=Float.parseFloat(tempNum1)/Float.parseFloat(tempNum2);
						  			result.setText(Float.toString(tempResult));
						  			num1=new StringBuffer();
						  			num2=new StringBuffer();
						  			num1.append(Float.toString(tempResult));
						  			operationMode=0;
						  			isFirstNumber=true;
						  			break;
						  	}
						  	
					  }
					  else
					  {
					  
					  }
				}
	        });
	  	  	Drawable alpha1 = clearBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	 	alpha1 = plusBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = minusBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = divBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = multiBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = equalBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	
	      	Button s1Btn=(Button)findViewById(R.id.s1);
	      	Button s2Btn=(Button)findViewById(R.id.s2);
	      	Button s3Btn=(Button)findViewById(R.id.s3);
	      	Button dotBtn=(Button)findViewById(R.id.s4);
	      	dotBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					  if (v.getId() == R.id.s4)
					  {
						  if(isFirstNumber)
						  {
		      					 result.append(".");
		      					 num1.append(".");
						  }
						  else
						  {
		      					 result.append(".");
		      					 num2.append(".");
						  }
					  }
				}
	        });
	      	
	  	  	alpha1 = s1Btn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	
	  	  	alpha1 = s2Btn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = s3Btn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	  	  	alpha1 = dotBtn.getBackground();
	  	  	alpha1.setAlpha(0); //알파값 0
	 
    
    }
    
    
 
}



   
	
   
   
   
   
   
   
   
    
    
    
    
    
    
반응형
Comments