본문 바로가기
Android

Android / 간단한 계산기 앱 만들기

by LWM 2020. 7. 22.
반응형

간단한 계산기 앱을 만들어보자. 이번 포스팅 또한 심심풀이로 업로드한것이니 가볍게 보시길 바랍니다.

 

 

-계산기 앱 예제-

 

-코드-

 

필자는 eval()함수를 활용하여 문자열을 숫자로 파싱해서 계산기를 만들었다. 안드로이드는 eval()메서드를 지원하지않으므로 다음과 같이 gradle에 라이브러리를 추가하였다.

implementation 'io.apisense:rhino-android:1.1.1'

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    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"
    android:gravity="center">

    <TableRow android:gravity="center">

        <EditText
            android:id="@+id/pt_result"
            android:layout_span="4"
            android:hint="숫자를 입력하시오." />

    </TableRow>

    <TableRow android:gravity="center">

        <Button
            android:id="@+id/num1"
            android:text="1" />

        <Button
            android:id="@+id/num2"
            android:text="2" />

        <Button
            android:id="@+id/num3"
            android:text="3" />

        <Button
            android:id="@+id/btn_add"
            android:text="+" />
    </TableRow>

    <TableRow android:gravity="center">

        <Button
            android:id="@+id/num4"
            android:text="4" />

        <Button
            android:id="@+id/num5"
            android:text="5" />

        <Button
            android:id="@+id/num6"
            android:text="6" />

        <Button
            android:id="@+id/btn_minus"
            android:text="-" />
    </TableRow>

    <TableRow android:gravity="center">

        <Button
            android:id="@+id/num7"
            android:text="7" />

        <Button
            android:id="@+id/num8"
            android:text="8" />

        <Button
            android:id="@+id/num9"
            android:text="9" />

        <Button
            android:id="@+id/btn_multi"
            android:text="*" />
    </TableRow>

    <TableRow android:gravity="center">

        <Button
            android:id="@+id/num0"
            android:layout_span="1"
            android:text="0" />

        <Button
            android:id="@+id/btn_clear"
            android:text="c"
            android:textColor="@android:color/holo_red_dark" />

        <Button
            android:id="@+id/btn_divide"
            android:text="/" />

        <Button
            android:id="@+id/btn_cal"
            android:layout_span="1"
            android:text="="
            android:textColor="@android:color/holo_green_dark" />

    </TableRow>
</TableLayout>

 

MainActivity.java

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.test.Eval;
import com.example.test.R;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Main_Activity";
    private EditText ptResult;
    private Button btn[] = new Button[16];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initListener();
    }

    private void initListener() {
        for (int i=0; i<14; i++){
            btn[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button btn = (Button)v;
                    ptResult.append(btn.getText().toString());
                }
            });
        }

        btn[14].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ptResult.setText("");
            }
        });

        btn[15].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = ptResult.getText().toString();
                ptResult.setText(Eval.cal(result));
                Log.d(TAG, "initListener: result"+result);
            }
        });
    }

    private void init() {
        btn[0] = findViewById(R.id.num0);
        btn[1] = findViewById(R.id.num1);
        btn[2] = findViewById(R.id.num2);
        btn[3] = findViewById(R.id.num3);
        btn[4] = findViewById(R.id.num4);
        btn[5] = findViewById(R.id.num5);
        btn[6] = findViewById(R.id.num6);
        btn[7] = findViewById(R.id.num7);
        btn[8] = findViewById(R.id.num8);
        btn[9] = findViewById(R.id.num9);
        btn[10] = findViewById(R.id.btn_add);
        btn[11] = findViewById(R.id.btn_minus);
        btn[12] = findViewById(R.id.btn_multi);
        btn[13] = findViewById(R.id.btn_divide);
        btn[14] = findViewById(R.id.btn_clear);
        btn[15] = findViewById(R.id.btn_cal);
        ptResult = findViewById(R.id.pt_result);
    }
}

 

Eval.java

package com.example.test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Eval {
    public static String cal(String result) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        try {
            return engine.eval(result).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}

반응형