간단한 사칙 연산

이건 뭐 개발이라고 할 것도 없고 시험삼아 만들어본것.
에딧텍스트 3개와 버튼 4개로 구성.

아무리 발로 만들어도 0으로 나누는것 체크쯤이야 해주는게 매너~


cfile29.uf.15338E1A4CA0626E8D34B6.apk

[#M_Activity Class|접기|


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
    public void  btnClickHandler(View target){
     String result;
        EditText leftValue = (EditText)this.findViewById(R.id.EditText01);
        EditText rightValue = (EditText)this.findViewById(R.id.EditText02);
        EditText resultValue = (EditText)this.findViewById(R.id.EditText03);
     switch(target.getId()){
      case R.id.Button01:
       result = Integer.toString(Integer.parseInt(leftValue.getText().toString()) + Integer.parseInt(rightValue.getText().toString())); 
       break;
      case R.id.Button02:
       result = Integer.toString(Integer.parseInt(leftValue.getText().toString()) – Integer.parseInt(rightValue.getText().toString()));   
       break;
      case R.id.Button03:
       result = Integer.toString(Integer.parseInt(leftValue.getText().toString()) * Integer.parseInt(rightValue.getText().toString())); 
       break;
      case R.id.Button04:
       if(Integer.parseInt(rightValue.getText().toString()) == 0){
        result=”Can’t Div to zero”;
       }else{
        result = Double.toString(Double.parseDouble(leftValue.getText().toString()) / Double.parseDouble(rightValue.getText().toString()));
       }
       break;
      default:
       result = “Undefined”;
       break;
     }
     resultValue.setText(result);
    }
 
_M#]

[#M_Layout.xml|접기|


<LinearLayout android:id=”@+id/LinearLayout01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_weight=”1.0″>
 <EditText android:id=”@+id/EditText01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”@string/LeftValue” android:layout_weight=”1.0″></EditText>
 <EditText android:id=”@+id/EditText02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”@string/RightValue” android:layout_weight=”1.0″></EditText>
</LinearLayout>
<LinearLayout android:id=”@+id/LinearLayout02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:orientation=”vertical” android:layout_weight=”0.0″>
 <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:id=”@+id/Button01″ android:text=”+” android:layout_weight=”1.0″ android:onClick=”btnClickHandler”></Button>
 <Button android:id=”@+id/Button02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”-” android:layout_weight=”1.0″ android:onClick=”btnClickHandler”></Button>
 <Button android:text=”*” android:id=”@+id/Button03″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_weight=”1.0″ android:onClick=”btnClickHandler”></Button>
 <Button android:text=”/” android:id=”@+id/Button04″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_weight=”1.0″ android:onClick=”btnClickHandler”></Button>
</LinearLayout>
<LinearLayout android:id=”@+id/LinearLayout03″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_weight=”1.0″>
 <EditText android:id=”@+id/EditText03″ android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”@string/ResultValue” android:layout_weight=”1.0″></EditText>
</LinearLayout>


_M#]

댓글 남기기