독도갈매기의 개발 블로그
[안드로이드] 계산기 만들기 -2- 기능 구현 본문
전편과 이어지는 포스팅입니다. 계산기 만들기 -1- Layout 안읽으셨다면 꼭 읽고 와주세요!
기능 담당 코드
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// input값을 받는 String변수를 선언 합니다.
String input = "";
// 연산 할 때 쓰일 firstNum, secondNum 변수를 선언 합니다.
int firstNum = 0;
int secondNum = 0;
// 간단하게 결과를 줄지 안줄지를 결정하는 boolean 변수입니다.
boolean rescheck = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 값을 입력하는 메서드들을 정의해줍니다.
public void pushOneNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox); // inputBox라는 이름을 가진 TextView를 가져와 변수에 담습니다.
input += "1"; // 문자열에 +=을 사용해 나중에 사용하기 위해 계속 쌓아둡니다.
inputView.setText(input); // setText라는 메서드를 사용해 값을 input값으로 세팅해줍니다.
}
public void pushTwoNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "2";
inputView.setText(input);
}
public void pushThreeNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "3";
inputView.setText(input);
}
public void pushFourNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "4";
inputView.setText(input);
}
public void pushFiveNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "5";
inputView.setText(input);
}
public void pushSixNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "6";
inputView.setText(input);
}
public void pushSevenNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "7";
inputView.setText(input);
}
public void pushEightNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "8";
inputView.setText(input);
}
public void pushNineNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "9";
inputView.setText(input);
}
public void pushZeroNum(View v) {
TextView inputView = (TextView) findViewById(R.id.inputBox);
input += "0";
inputView.setText(input);
}
// 사칙연산을 처리하고 화면에 뿌려주는 역할을 하는 메서드를 생성해줍니다.
public void plusNumber(View v) {
// 사칙연산 버튼을 처음 누르는 것인지 확인합니다.
if(rescheck == false) {
firstNum = Integer.parseInt(input); // 값을 int형으로 형변환 하여 변수에 담습니다.
input = "";
rescheck = true;
// TextView 받아오기
TextView opcheck = (TextView) findViewById(R.id.opcheck);
TextView inputView = (TextView) findViewById(R.id.inputBox);
// 글자 크기 조절
opcheck.setTextSize(15);
// 화면에 뿌려주기
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("값을 입력한 뒤 더하기 버튼을 한번 더 눌러주세요!");
} else {
secondNum = Integer.parseInt(input);
// TextView 받아오기
TextView resultBox = (TextView) findViewById(R.id.resultBox);
TextView inputView = (TextView) findViewById(R.id.inputBox);
TextView opcheck = (TextView) findViewById(R.id.opcheck);
// 값 계산
int result = firstNum + secondNum; // firstNum과 secondNum을 연산하여 result변수에 값을 담습니다.
// 화면에 뿌려주기
resultBox.setText(Integer.toString(result));
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("간단한 계산기 앱 00000 홍길동");
// 글자 크기 조절
opcheck.setTextSize(20);
// 초기화
rescheck = false;
firstNum = 0;
secondNum = 0;
input = "";
}
}
public void minusNumber(View v) {
if(rescheck == false) {
firstNum = Integer.parseInt(input);
input = "";
rescheck = true;
TextView opcheck = (TextView) findViewById(R.id.opcheck);
TextView inputView = (TextView) findViewById(R.id.inputBox);
opcheck.setTextSize(15);
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("값을 입력한 뒤 빼기 버튼을 한번 더 눌러주세요!");
} else {
secondNum = Integer.parseInt(input);
TextView resultBox = (TextView) findViewById(R.id.resultBox);
TextView inputView = (TextView) findViewById(R.id.inputBox);
TextView opcheck = (TextView) findViewById(R.id.opcheck);
opcheck.setTextSize(20);
int result = firstNum - secondNum;
resultBox.setText(Integer.toString(result));
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("간단한 계산기 앱 00000 홍길동");
rescheck = false;
firstNum = 0;
secondNum = 0;
input = "";
}
}
public void multiNum(View v) {
if(rescheck == false) {
firstNum = Integer.parseInt(input);
input = "";
rescheck = true;
TextView opcheck = (TextView) findViewById(R.id.opcheck);
TextView inputView = (TextView) findViewById(R.id.inputBox);
opcheck.setTextSize(15);
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("값을 입력한 뒤 곱하기 버튼을 한번 더 눌러주세요!");
} else {
secondNum = Integer.parseInt(input);
TextView resultBox = (TextView) findViewById(R.id.resultBox);
TextView inputView = (TextView) findViewById(R.id.inputBox);
TextView opcheck = (TextView) findViewById(R.id.opcheck);
opcheck.setTextSize(20);
int result = firstNum * secondNum;
resultBox.setText(Integer.toString(result));
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("간단한 계산기 앱 00000 홍길동");
rescheck = false;
firstNum = 0;
secondNum = 0;
input = "";
}
}
public void divideNumber(View v) {
if(rescheck == false) {
firstNum = Integer.parseInt(input);
input = "";
rescheck = true;
TextView opcheck = (TextView) findViewById(R.id.opcheck);
TextView inputView = (TextView) findViewById(R.id.inputBox);
opcheck.setTextSize(15);
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("값을 입력한 뒤 나누기 버튼을 한번 더 눌러주세요!");
} else {
secondNum = Integer.parseInt(input);
TextView resultBox = (TextView) findViewById(R.id.resultBox);
TextView inputView = (TextView) findViewById(R.id.inputBox);
TextView opcheck = (TextView) findViewById(R.id.opcheck);
opcheck.setTextSize(20);
int result = firstNum / secondNum;
resultBox.setText(Integer.toString(result));
inputView.setText("버튼을 눌러 값을 입력해주세요");
opcheck.setText("간단한 계산기 앱 00000 홍길동");
rescheck = false;
firstNum = 0;
secondNum = 0;
input = "";
}
}
}
버튼을 눌러 값을 입력하는 부분을 숫자마다 메서드를 만드는 식으로 구현했습니다. 다른 방법이 마땅히 떠오르지 않아서...
사칙연산 부분은 boolean 변수인 rescheck를 만들어 한번에 한가지 연산만 가능하게 구현했습니다.
딱 한가지 연산과 딱 한가지 식만 가능하게 구현하는 것은 별로 어렵지 않아 각 연산마다 생성해주었습니다.
안드로이는 자신있는 분야가 아니라 자세한 설명은 주석을 읽어주시면 감사하겠습니다. (_._)
아 그리고 주석은 비슷한 메서드가 많아 처음 등장하는 메서드만 달아 놓았습니다.
오늘도 부족한 제 포스팅을 읽어주셔서 감사합니다!
'안드로이드' 카테고리의 다른 글
[안드로이드] 계산기 만들기 -1- Layout (0) | 2020.10.04 |
---|
Comments