개발/Android

[Android Studio] 계산기 만들기

Hyunsun 2021. 10. 7. 00:00
728x90

안드로이드 스튜디오로 탁상용 계산기 만들기

 

테이블레이아웃을 이용하여 위젯을 배치합니다.

테이블레이아웃은 위젯을 표 형태로 배치할 때 주로 활용됩니다.

<TableRow>의 수는 행의 수 이고, 열의 수는 <TableRow>안에 포함된 위젯의 수로 결정됩니다.

 

코드

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="7dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#aaaaaa"
        android:padding="5dp"
        android:textSize="30dp" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/n1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="1"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="2"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="3"
                android:textSize="25dp" />

            <Button
                android:id="@+id/plus"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="+"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/n4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="4"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="5"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="6"
                android:textSize="25dp" />

            <Button
                android:id="@+id/sub"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="-"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/n7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="7"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="8"
                android:textSize="25dp" />

            <Button
                android:id="@+id/n9"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="9"
                android:textSize="25dp" />

            <Button
                android:id="@+id/mul"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="*"
                android:textSize="25dp" />
        </TableRow>

        <TableRow android:layout_weight="1">

            <Button
                android:id="@+id/n0"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="0"
                android:textSize="25dp" />

            <Button
                android:id="@+id/ndot"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="."
                android:textSize="30dp" />

            <Button
                android:id="@+id/clear"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="clear"
                android:textSize="20dp" />

            <Button
                android:id="@+id/div"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="onClick"
                android:text="/"
                android:textSize="25dp" />
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/Ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="OK"
        android:textSize="30dp" />

</LinearLayout>

 

MainActivity.java

package com.cookandroid.test;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private double storedValue;
    private char curOperator;

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

        text = findViewById(R.id.text);
    }

    public void onClick(View view) {
        String current = text.getText().toString();

        switch (view.getId()) {
            case R.id.n0:
                text.setText(current + "0");
                break;
            case R.id.n1:
                text.setText(current + "1");
                break;
            case R.id.n2:
                text.setText(current + "2");
                break;
            case R.id.n3:
                text.setText(current + "3");
                break;
            case R.id.n4:
                text.setText(current + "4");
                break;
            case R.id.n5:
                text.setText(current + "5");
                break;
            case R.id.n6:
                text.setText(current + "6");
                break;
            case R.id.n7:
                text.setText(current + "7");
                break;
            case R.id.n8:
                text.setText(current + "8");
                break;
            case R.id.n9:
                text.setText(current + "9");
                break;
            case R.id.ndot:
                text.setText(current + ".");
                break;

 

숫자를 먼저 입력하지 않고 연산자 버튼 또는 OK 버튼을 누르면 에러가 나서 앱이 강제 종료되는데, 이를 방지하기 위하여 토스트(Toast)로 예외처리를 합니다.

 

case 안에 if문을 사용하여 토스트로 에러 메세지를 표시해줍니다.

 

"Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();"

 

            case R.id.plus:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '+';
                    text.setText("");
                    break;
                }
            case R.id.sub:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '-';
                    text.setText("");
                    break;
                }
            case R.id.mul:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '*';
                    text.setText("");
                    break;
                }
            case R.id.div:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    storedValue = Double.parseDouble(current);
                    curOperator = '/';
                    text.setText("");
                    break;
                }

            case R.id.clear:
                text.setText("");
                storedValue = 0.0;
                break;

            case R.id.Ok:
                if (current.equals("")) {
                    Toast.makeText(getApplicationContext(), "숫자를 먼저 입력하세요", Toast.LENGTH_SHORT).show();
                } else {
                    double result = 0;
                    double thisValue = Double.parseDouble(text.getText().toString());
                    switch (curOperator) {
                        case '+':
                            result = storedValue + thisValue;
                            break;
                        case '-':
                            result = storedValue - thisValue;
                            break;
                        case '*':
                            result = storedValue * thisValue;
                            break;
                        case '/':
                            result = storedValue / thisValue;
                            break;
                    }
                    text.setText("" + result);
                    storedValue = 0.0;
                    break;
                }
        }
    }
}

 

결과

728x90

'개발 > Android' 카테고리의 다른 글

[Android Studio] 간단 그림판 만들기  (0) 2021.11.17
[Android Studio] Layout 종류  (0) 2021.11.16
[Android Studio] 비만도 계산기 만들기  (0) 2021.10.04