본문 바로가기
category/android studio 앱 개발

android studio 계산기 #1

by 자운대고라니 2021. 3. 29.
반응형

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?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:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="50dp"
    android:stretchColumns="*"
    tools:context=".MainActivity">
 
    <TableLayout
        android:layout_height="0dp"
        android:layout_weight="1">
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
 
            <TextView
                android:id="@+id/et_result2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="this place is result"
                android:textSize="20sp" />
 
        </TableRow>
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
            <TextView />
 
        </TableRow>
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
            <TextView
                android:id="@+id/et_result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:hint="your cal space" />
 
        </TableRow>
 
        <TableRow
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="0dp">
            <TextView />
        </TableRow>
    </TableLayout>
 
    <TableLayout
        android:layout_height="0dp"
        android:layout_weight="5">
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_clear"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="C" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_left"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="(" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_right"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text=")" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_per"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="%" />
        </TableRow>
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_7"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="7" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_8"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="8" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_9"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="9" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_plus"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="+" />
        </TableRow>
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_4"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="4" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_5"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="5" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_6"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="6" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_minus"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="-" />
        </TableRow>
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/num_1"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="1" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="2" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_3"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="3" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_multiple"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="*" />
        </TableRow>
        
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_dot"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="." />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/num_0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="0" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_result"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="=" />
            <TextView
                android:layout_width="10dp"
                android:layout_height="match_parent"/>
            <Button
                android:id="@+id/btn_slush"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:autoSizeTextType="uniform"
                android:text="/" />
        </TableRow>
 
 
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5"></TableRow>
 
    </TableLayout>
 
 
 
 
 
</TableLayout>
cs

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.example.calculator;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Main_Activity";
 
    private TextView ptResult;
    private TextView ptResult2;
    private String s;
    private Button btn[] = new Button[20];
    private int i=0;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        init();
        initListener();
    }
 
    private void init(){
        btn[0]=findViewById(R.id.num_0);
        btn[1]=findViewById(R.id.num_1);
        btn[2]=findViewById(R.id.num_2);
        btn[3]=findViewById(R.id.num_3);
        btn[4]=findViewById(R.id.num_4);
        btn[5]=findViewById(R.id.num_5);
        btn[6]=findViewById(R.id.num_6);
        btn[7]=findViewById(R.id.num_7);
        btn[8]=findViewById(R.id.num_8);
        btn[9]=findViewById(R.id.num_9);
 
        btn[10]=findViewById(R.id.btn_plus);
        btn[11]=findViewById(R.id.btn_minus);
        btn[12]=findViewById(R.id.btn_multiple);
        btn[13]=findViewById(R.id.btn_slush);
        btn[14]=findViewById(R.id.btn_dot);
        btn[15]=findViewById(R.id.btn_per);
        btn[16]=findViewById(R.id.btn_left);
        btn[17]=findViewById(R.id.btn_right);
        btn[18]=findViewById(R.id.btn_clear);
        btn[19]=findViewById(R.id.btn_result);
        ptResult=findViewById(R.id.et_result);
        ptResult2=findViewById(R.id.et_result2);
    }
 
    private void initListener(){
        for (int i=1; i<10;i++){
            int I = i;
            btn[i].setOnClickListener((View view)-> {
                Button btn = (Button) view;
                ptResult.append(btn.getText().toString());
 
                String result3 = ptResult.getText().toString();
                double a = Double.valueOf(Eval.cal(result3));
                int b = (int) a;
 
                if(a == b) {
                    ptResult2.setText(Integer.toString(b));
                }
                else ptResult2.setText(Eval.cal(result3));
 
                s += btn.getText().toString();
            });
        }
 
        for (int i=10; i<16; i++) {
            btn[i].setOnClickListener((View view)-> {
                Button btn = (Button) view;
                ptResult.append(btn.getText().toString());
                s = "";
            });
        }
 
        btn[0].setOnClickListener((View view)-> {
            ptResult.append("0");
            ptResult2.append("0");
        });
        btn[16].setOnClickListener((View view) -> {
            ptResult.append("(");
            left_right();
        });
        btn[18].setOnClickListener((View view)-> {
            ptResult.setText("");
            ptResult2.setText("");
        });
 
        btn[19].setOnClickListener((View view)-> {
            String result2=ptResult.getText().toString();
            ptResult2.setText(Eval.cal(result2));
            ptResult.setText(Eval.cal(result2));
 
            String result3 = ptResult.getText().toString();
            double a = Double.valueOf(Eval.cal(result3));
            int b = (int) a;
 
            if(a == b) {
                ptResult.setText(Integer.toString(b));
                ptResult2.setText(Integer.toString(b));
            }
            else {
                ptResult.setText(Eval.cal(result3));
                ptResult2.setText(Eval.cal(result3));
            }
 
        });
    }
    private void left_right() {
        for (int i=1; i<10;i++){
            int I = i;
            btn[i].setOnClickListener((View view)-> {
                Button btn = (Button) view;
                ptResult.append(btn.getText().toString());
                s += btn.getText().toString();
            });
        }
 
        for (int i=10; i<16; i++) {
            btn[i].setOnClickListener((View view)-> {
                Button btn = (Button) view;
                ptResult.append(btn.getText().toString());
                s = "";
            });
        }
        btn[16].setOnClickListener((View view) -> {
            ptResult.append("(");
            left_right();
        });
        btn[17].setOnClickListener((View view) -> {
            ptResult.append(")");
            String result3 = ptResult.getText().toString();
            double a = Double.valueOf(Eval.cal(result3));
            int b = (int) a;
 
            if(a == b) {
                ptResult2.setText(Integer.toString(b));
            }
            else ptResult2.setText(Eval.cal(result3));
 
            initListener();
        });
        btn[0].setOnClickListener((View view)-> {
            ptResult.append("0");
            ptResult2.append("0");
        });
 
        btn[18].setOnClickListener((View view)-> {
            ptResult.setText("");
            ptResult2.setText("");
        });
    }
}
cs

Eval.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.calculator;
 
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;
    }
}
cs

결과

정상적으로 작동하지만, 오류가 있다.

 

1. 괄호 연산이 오류가 있으면 앱이 닫힌다. 예를 들어서 ( 보다 ) 를 먼저 입력하면 앱이 닫힌다.

ex. 1+2+3) 입력시 앱이 닫힌다.

 

2. 연산자를 동시에 못 쓰도록 바꿔야한다.

ex. ++++ 입력 가능


수정할 부분

 

1. 주석처리를 너무 안 했다.

2. MainActivity.java 코드 중 불필요한 라인이 존재한다.

3. 위의 오류 사항을 수정해야한다.

4. 디자인 부분 개선

5. 지우기 기능 추가 (back space)


참고 블로그 itstudy-mary.tistory.com/179

 

안드로이드 스튜디오,java] 간단한 계산기 앱 만들기

출력 결과 : 안드로이드 스튜디오를 접한 사람이라면 먼저 자바나 코틀린을 접했을 것이다. 자바의 간단한 계산기는 eval함수를 이용하여 문자열을 계산해준다. 그 함수는 javax 함수 위에 있는데,

itstudy-mary.tistory.com

 

위의 코드를 실행시키기 위해선 밑의 사이트에 접속 후

jar 파일 다운로드 -> library에 추가해야 Eval class가 정상적으로 작동할 수 있다.

ScriptEngine, ScriptEngineManager download www.java2s.com/Code/Jar/j/Downloadjsr223jar.htm

 

Download jsr223.jar : jsr223 « j « Jar File Download

 

www.java2s.com

 

반응형

댓글