07-28 Cal Code

Ruinak·2021년 7월 28일
0

Android Lesson

목록 보기
12/26
post-thumbnail

계산기 라이브러리

  • implementation 'io.apisense:rhino-android:1.0'

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    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:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#485FB3">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cal"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_marginStart="10dp"/>

    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <EditText
            android:id="@+id/etCount"
            android:layout_width="351dp"
            android:layout_height="52dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <Button
            android:id="@+id/btn1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="1"
            app:layout_constraintStart_toStartOf="@+id/etCount"
            app:layout_constraintTop_toBottomOf="@+id/etCount" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="2"
            app:layout_constraintStart_toEndOf="@+id/btn1"
            app:layout_constraintTop_toTopOf="@+id/btn1" />
        <Button
            android:id="@+id/btn3"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="3"
            app:layout_constraintStart_toEndOf="@+id/btn2"
            app:layout_constraintTop_toTopOf="@+id/btn2" />
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="+"
            app:layout_constraintEnd_toEndOf="@+id/etCount"
            app:layout_constraintStart_toEndOf="@+id/btn3"
            app:layout_constraintTop_toTopOf="@+id/btn3" />
        <Button
            android:id="@+id/btn4"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="4"
            app:layout_constraintStart_toStartOf="@+id/btn1"
            app:layout_constraintTop_toBottomOf="@+id/btn1" />
        <Button
            android:id="@+id/btn5"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="5"
            app:layout_constraintStart_toStartOf="@+id/btn2"
            app:layout_constraintTop_toBottomOf="@+id/btn1" />
        <Button
            android:id="@+id/btn6"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="6"
            app:layout_constraintStart_toStartOf="@+id/btn3"
            app:layout_constraintTop_toTopOf="@+id/btn5" />
        <Button
            android:id="@+id/btnMinus"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="-"
            app:layout_constraintStart_toStartOf="@+id/btnAdd"
            app:layout_constraintTop_toTopOf="@+id/btn6" />
        <Button
            android:id="@+id/btn7"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="7"
            app:layout_constraintStart_toStartOf="@+id/btn4"
            app:layout_constraintTop_toBottomOf="@+id/btn4" />
        <Button
            android:id="@+id/btn8"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="8"
            app:layout_constraintStart_toStartOf="@+id/btn5"
            app:layout_constraintTop_toTopOf="@+id/btn7" />
        <Button
            android:id="@+id/btn9"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="9"
            app:layout_constraintStart_toStartOf="@+id/btn6"
            app:layout_constraintTop_toTopOf="@+id/btn8" />
        <Button
            android:id="@+id/btnDup"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="*"
            app:layout_constraintStart_toStartOf="@+id/btnMinus"
            app:layout_constraintTop_toTopOf="@+id/btn9" />
        <Button
            android:id="@+id/btn0"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="0"
            app:layout_constraintStart_toStartOf="@+id/btn7"
            app:layout_constraintTop_toBottomOf="@+id/btn7" />
        <Button
            android:id="@+id/btnClear"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="C"
            android:textColor="@android:color/holo_red_dark"
            app:layout_constraintStart_toStartOf="@+id/btn8"
            app:layout_constraintTop_toTopOf="@+id/btn0" />
        <Button
            android:id="@+id/btnDivide"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="/"
            app:layout_constraintStart_toStartOf="@+id/btn9"
            app:layout_constraintTop_toTopOf="@+id/btnClear" />
        <Button
            android:id="@+id/btnEqual"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="="
            app:layout_constraintStart_toStartOf="@+id/btnDup"
            app:layout_constraintTop_toTopOf="@+id/btnDivide" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

MainActivity

package com.com.cal;

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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity2";

    private EditText etCount;
    // 배열로 버튼을 선언하는 것이 훨씬 간결하고 편하다.
    private Button[] btn = new Button[16];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: Start");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initListener();
    }

    // View를 찾는 함수
    private void init(){
        etCount =findViewById(R.id.etCount);
        btn[0] = findViewById(R.id.btn0);
        btn[1] = findViewById(R.id.btn1);
        btn[2] = findViewById(R.id.btn2);
        btn[3] = findViewById(R.id.btn3);
        btn[4] = findViewById(R.id.btn4);
        btn[5] = findViewById(R.id.btn5);
        btn[6] = findViewById(R.id.btn6);
        btn[7] = findViewById(R.id.btn7);
        btn[8] = findViewById(R.id.btn8);
        btn[9] = findViewById(R.id.btn9);
        btn[10] = findViewById(R.id.btnAdd);
        btn[11] = findViewById(R.id.btnMinus);
        btn[12] = findViewById(R.id.btnDup);
        btn[13] = findViewById(R.id.btnDivide);
        btn[14] = findViewById(R.id.btnClear);
        btn[15] = findViewById(R.id.btnEqual);
    }

    private void initListener(){
        for(int i = 0; i <14; i++){
            btn[i].setOnClickListener(this);
        }
        btn[14].setOnClickListener(v ->{
            String result = "";
            etCount.setText(result);
        });
        btn[15].setOnClickListener(v ->{
            String setData = etCount.getText().toString();
            String result = Eval.cal(setData);
            etCount.setText(result);
        });
    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v; // 다 버튼형태이므로 버튼으로 통일 가능
        Log.d(TAG, "onClick: " + btn.getText());
        etCount.append(btn.getText());
    }
}

Eval

package com.com.cal;

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

public class Eval {
    public static String cal(String result){
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");

        try {
            return engine.eval(result).toString();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }
}
profile
Nil Desperandum <절대 절망하지 마라>

0개의 댓글