2012. 8. 23. 21:05
안드로이드 - 계산기 예제 안드로이드 이야기2012. 8. 23. 21:05
728x90
반응형
안드로이드 - 계산기 예제
2011/07/16 14:58
http://blog.naver.com/ijoos/60134507556
자바소스
package com.android.park; import android.R.integer; 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 TestCounterActivity extends Activity { private TextView resultValue; private EditText sol; private int leftValue = 0; private int RightValue = 0; private int what = 0; private int Result = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sol = (EditText) findViewById(R.id.sol); resultValue = (TextView) findViewById(R.id.result); Button btnzero = (Button) findViewById(R.id.zero); btnzero.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("0"); else sol.setText("0"); } }); Button btnone = (Button) findViewById(R.id.one); btnone.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("1"); else sol.setText("1"); } }); Button btntwo = (Button) findViewById(R.id.two); btntwo.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("2"); else sol.setText("2"); } }); Button btnthree = (Button) findViewById(R.id.three); btnthree.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("3"); else sol.setText("3"); } }); Button btnfour = (Button) findViewById(R.id.four); btnfour.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("4"); else sol.setText("4"); } }); Button btnfive = (Button) findViewById(R.id.five); btnfive.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("5"); else sol.setText("5"); } }); Button btnsix = (Button) findViewById(R.id.six); btnsix.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("6"); else sol.setText("6"); } }); Button btnseven = (Button) findViewById(R.id.seven); btnseven.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("7"); else sol.setText("7"); } }); Button btneight = (Button) findViewById(R.id.eight); btneight.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("8"); else sol.setText("8"); } }); Button btnnine = (Button) findViewById(R.id.nine); btnnine.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (sol != null) sol.append("9"); else sol.setText("9"); } }); Button btnplus = (Button) findViewById(R.id.plus); btnplus.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { leftValue = Integer.parseInt(sol.getText().toString()); sol.setText(""); what = 0; } }); Button btnmin = (Button) findViewById(R.id.min); btnmin.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { leftValue = Integer.parseInt(sol.getText().toString()); sol.setText(""); what = 1; } }); Button btnmul = (Button) findViewById(R.id.mul); btnmul.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { leftValue = Integer.parseInt(sol.getText().toString()); sol.setText(""); what = 2; } }); Button btndiv = (Button) findViewById(R.id.div); btndiv.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { leftValue = Integer.parseInt(sol.getText().toString()); sol.setText(""); what = 3; } }); Button btnclr = (Button) findViewById(R.id.clr); btnclr.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { clearAll(); } }); Button btnequal = (Button) findViewById(R.id.equal); btnequal.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (what == 0) { RightValue = Integer.parseInt(sol.getText().toString()); Result = leftValue + RightValue; resultValue.setText(Integer.toString(Result)); clearAll(); } else if (what == 1) { RightValue = Integer.parseInt(sol.getText().toString()); Result = leftValue - RightValue; resultValue.setText(Integer.toString(Result)); clearAll(); } else if (what == 2) { RightValue = Integer.parseInt(sol.getText().toString()); Result = leftValue * RightValue; resultValue.setText(Integer.toString(Result)); clearAll(); } else if (what == 3) { RightValue = Integer.parseInt(sol.getText().toString()); Result = leftValue / RightValue; resultValue.setText(Integer.toString(Result)); clearAll(); } } }); } public void clearAll() { leftValue = 0; RightValue = 0; sol.setText(""); } } |
XML소스
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <EditText android:id="@+id/sol" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:text="결과값: " android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_below="@+id/sol" android:layout_alignLeft="@+id/sol"></TextView> <TextView android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:id="@+id/result" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textView1" android:layout_alignTop="@+id/textView1" android:layout_alignBottom="@+id/textView1"></TextView> <Button android:id="@+id/zero" android:layout_width="80dip" android:layout_height="80dip" android:layout_alignParentBottom="true" android:text="0" /> <Button android:id="@+id/clr" android:layout_width="80dip" android:layout_height="80dip" android:layout_alignBottom="@id/zero" android:layout_toRightOf="@id/zero" android:text="C" /> <Button android:id="@+id/equal" android:layout_width="80dip" android:layout_height="80dip" android:layout_alignBottom="@id/zero" android:layout_toRightOf="@id/clr" android:text="=" /> <Button android:id="@+id/div" android:layout_width="80dip" android:layout_height="80dip" android:layout_alignBottom="@id/zero" android:layout_toRightOf="@id/equal" android:text="/" /> <Button android:id="@+id/one" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/zero" android:text="1" /> <Button android:id="@+id/two" android:layout_width="80dip" android:layout_height="80dip" android:layout_toRightOf="@id/one" android:layout_above="@id/clr" android:text="2" /> <Button android:id="@+id/three" android:layout_width="80dip" android:layout_height="80dip" android:layout_toRightOf="@id/two" android:layout_above="@id/equal" android:text="3" /> <Button android:id="@+id/mul" android:layout_width="80dip" android:layout_height="80dip" android:layout_toRightOf="@id/three" android:layout_above="@id/div" android:text="*" /> <Button android:id="@+id/four" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/one" android:text="4" /> <Button android:id="@+id/five" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/two" android:layout_toRightOf="@id/four" android:text="5" /> <Button android:id="@+id/six" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/three" android:layout_toRightOf="@id/five" android:text="6" /> <Button android:id="@+id/min" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/mul" android:layout_toRightOf="@id/six" android:text="-" /> <Button android:id="@+id/seven" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/four" android:text="7" /> <Button android:id="@+id/eight" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/five" android:layout_toRightOf="@id/seven" android:text="8" /> <Button android:id="@+id/nine" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/six" android:layout_toRightOf="@id/eight" android:text="9" /> <Button android:id="@+id/plus" android:layout_width="80dip" android:layout_height="80dip" android:layout_above="@id/min" android:layout_toRightOf="@id/nine" android:text="+" /> </RelativeLayout> |
뭐 섬세하게 0으로 나눈다거나, 값없이 =을 누른다거나, Integer값을 넘긴다던가의 처리같은건 하지 않았습니다. 이걸 토대로 뭔가 굉장한 계산기를 만들어 보는 것도 좋은 과제가 될 수 있겠네요. 안드로이드 개발 카페에 올려있는 코드를 수정하여 정상적인 구동하게 만들어 놓고 제 블로그에 올립니다. 수고하세요.^^
[출처] [안드로이드] 계산기 예제|작성자 ijoos
728x90
반응형
'안드로이드 이야기' 카테고리의 다른 글
안드로이드 - 배터리모니터링샘플소스 (0) | 2012.08.23 |
---|---|
안드로이드 - animation Drawable , 움직이는 그림 만들기 (0) | 2012.08.23 |
안드로이드 - setHeight에서 dip 값을 쓰는 방법 (0) | 2012.08.23 |
안드로이드 - Android Device IP받아오기 (0) | 2012.08.23 |
안드로이드 - 로딩 시 화면 띄우기 (0) | 2012.08.23 |