본문 바로가기
TIL/Android

[Android] 계산기 앱 만들기

by 왁왁s 2022. 10. 5.

안드로이드(Android) 계산기 앱 만들기

2개의 숫자를 입력받아 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 계산할 수 있는 간편한 계산기를 만들고자 한다.

 


완성된 안드로이드 유저 인터페이스

 

 

 

activity_main.xml 코드

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Number"
        />
    <EditText
        android:id="@+id/firstNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Second Number"
        />
    <EditText
        android:id="@+id/secondNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="+"
        />
    <Button
        android:id="@+id/btnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="-"
        />
    <Button
        android:id="@+id/btnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="*"
        />
    <Button
        android:id="@+id/btnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="/"
        />
    <TextView
        android:id="@+id/textResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Result"
        />
    <EditText
        android:id="@+id/resultNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

 

 


 

MainActivity.java 코드

package ex.app.calcaulator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText firstNum, secondNum;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView textResult;
    String firN, secN;
    Integer result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("간편 계산기");

        firstNum = (EditText) findViewById(R.id.firstNumber);
        secondNum = (EditText) findViewById(R.id.secondNumber);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnSub = (Button) findViewById(R.id.btnSub);
        btnMul = (Button) findViewById(R.id.btnMul);
        btnDiv = (Button) findViewById(R.id.btnDiv);

        textResult = (TextView) findViewById(R.id.textResult);

        btnAdd.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                firN = firstNum.getText().toString();
                secN = secondNum.getText().toString();
                result = Integer.parseInt(firN) + Integer.parseInt(secN);
                textResult.setText("계산 결과 : " + result.toString());
                return false;
            }
        });
        btnSub.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                firN = firstNum.getText().toString();
                secN = secondNum.getText().toString();
                result = Integer.parseInt(firN) - Integer.parseInt(secN);
                textResult.setText("계산 결과 : " + result.toString());
                return false;
            }
        });
        btnMul.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                firN = firstNum.getText().toString();
                secN = secondNum.getText().toString();
                result = Integer.parseInt(firN) * Integer.parseInt(secN);
                textResult.setText("계산 결과 : " + result.toString());
                return false;
            }
        });
        btnDiv.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                firN = firstNum.getText().toString();
                secN = secondNum.getText().toString();
                result = Integer.parseInt(firN) / Integer.parseInt(secN);
                textResult.setText("계산 결과 : " + result.toString());
                return false;
            }
        });


    }

}

 

 


 

 

간편 계산기 앱 구동 화면

 

댓글