매우 간단하고도 기본적이지만 필수적인 어플이다.
안드로이드 스튜디오를 먼저 실행해주고, 새 프로젝트 (이름: calculator in my case)를 생성해준다.
긴 말 필요없이 일단 코드부터!
[activity-main.xml]
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.393" />
<TableLayout
android:layout_width="355dp"
android:layout_height="302dp"
android:layout_marginEnd="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_result"
app:layout_constraintVertical_bias="0.623">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="+" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/button_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="-" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/button_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="*" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/button_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="=" />
<Button
android:id="@+id/button_div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="/" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
xml 코드에대해 전반적인 설명을 하자면,
constraintLayout에 TableLayout을 붙여, 그 밑으로 TableRow를 만들고 각 TableRow마다 Button을 생성해,
우리가 아는 계산기의 모습을 만들어 준다. (숫자(0~9),연산자)
text_result는 계산 결과 값과 입력값을 보여주기 위한 텍스트이다.
이는 MainActivity.java에서 기능을 추가해줌으로써 완성될 것이다.
[MainActivity.java]
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int opnd = 0; //입력값
private int value0 = 0; //연산을 할때, 첫번째 입력값
private int value1 = 0; //연산을 할때, 두번째 입력값
private int result = 0; //연산을 하고난, 결과값
private int where = 0; //연산을 할때, 연산자가 무엇인지
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txtResult = (TextView) findViewById(R.id.text_result);
((Button) findViewById(R.id.button_0)).setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
appendDigit(0); //0을 추가해줌. 자세한 건 appendDigit 함수로 가서 보자!
}
}
);
((Button) findViewById(R.id.button_1)).setOnClickListener(
(v) -> {
appendDigit(1);
}
);
((Button) findViewById(R.id.button_2)).setOnClickListener(
(v) -> {
appendDigit(2);
}
);
((Button) findViewById(R.id.button_3)).setOnClickListener(
(v) -> {
appendDigit(3);
}
);
((Button) findViewById(R.id.button_4)).setOnClickListener(
(v) -> {
appendDigit(4);
}
);
((Button) findViewById(R.id.button_5)).setOnClickListener(
(v) -> {
appendDigit(5);
}
);
((Button) findViewById(R.id.button_6)).setOnClickListener(
(v) -> {
appendDigit(6);
}
);
((Button) findViewById(R.id.button_7)).setOnClickListener(
(v) -> {
appendDigit(7);
}
);
((Button) findViewById(R.id.button_8)).setOnClickListener(
(v) -> {
appendDigit(8);
}
);
((Button) findViewById(R.id.button_9)).setOnClickListener(
(v) -> {
appendDigit(9);
}
);
((Button) findViewById(R.id.button_c)).setOnClickListener(
// 'c'버튼을 누르면 입력값 초기화, 다시 받음.
(v) -> {
opnd = 0;
value0 = 0;
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText("0");
}
);
((Button) findViewById(R.id.button_plus)).setOnClickListener(
// '+'버튼을 누르면 value0에 현재 입력값 저장, 새로운 입력값 받음. where에 특정값 저장. 나중에 식별할 수 있도록
(v) -> {
value0 = opnd;
opnd=0;
where = v.getId();
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText("");
}
);
((Button) findViewById(R.id.button_sub)).setOnClickListener(
// '- '버튼을 누르면 value0에 현재 입력값 저장, 새로운 입력값 받음. where에 특정값 저장. 나중에 식별할 수 있도록
(v) -> {
value0 = opnd;
opnd=0;
where = v.getId();
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText("");
}
);
((Button) findViewById(R.id.button_mul)).setOnClickListener(
// '*'버튼을 누르면 value0에 현재 입력값 저장, 새로운 입력값 받음. where에 특정값 저장. 나중에 식별할 수 있도록
(v) -> {
value0 = opnd;
opnd=0;
where = v.getId();
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText("");
}
);
((Button) findViewById(R.id.button_div)).setOnClickListener(
// '/ '버튼을 누르면 value0에 현재 입력값 저장, 새로운 입력값 받음. where에 특정값 저장. 나중에 식별할 수 있도록
(v) -> {
value0 = opnd;
opnd=0;
where = v.getId();
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText("");
}
);
((Button) findViewById(R.id.button_calc)).setOnClickListener( // '='버튼을 누르면 연산자를 파악해 입력값들을 계산해줌
(v) -> {
value1 = opnd;
switch(where){
case R.id.button_plus:
result = value0 + value1;
break;
case R.id.button_sub:
result = value0 - value1;
break;
case R.id.button_mul:
result = value0 * value1;
break;
case R.id.button_div:
result = value0 / value1;
break;
}
final TextView txtresult = (TextView) findViewById(R.id.text_result);
txtResult.setText(""+result);
}
);
}
private void appendDigit(int a) { //입력한 값을 왼쪽으로 밀어줌. ex) 2 버튼을 터치후, 0 버튼 터치하면 입력값은 20
opnd = opnd*10 + a;
final TextView txtResult = (TextView)findViewById(R.id.text_result);
txtResult.setText(""+opnd);
}
}
java 파일을 크게 설명하자면, 기능들이 각각 나뉘어져 있고, listener를 anonymous class로 만들어 넣어준다.
listener를 통해 버튼이 눌러졌을시, 함수가 callback된다.
이 글을 보시는 분들께 도움이 되길 바라며, 또 훗날의 내가 찾아왔을때 추억을 떠올리며 행복할 수 있길 바라며 글을 마친다.
또 다른 프로젝트로 곧 돌아오겠음. 안녕.
'모바일 프로그래밍' 카테고리의 다른 글
[diet & health application] (0) | 2021.06.06 |
---|---|
[Android Studio 사전 어플 만들기/landscape/가로화면/fragment] (0) | 2021.04.27 |
[Android Studio] 예약 어플 만들기/Calendar/TimePicker/AlertDialog (0) | 2021.04.17 |
안드로이드 스튜디오에서 그림판 만들기 (0) | 2021.04.10 |