Android studio programming

(22.12.04)Android ํ”„๋กœ๊ทธ๋ž˜๋ฐ : ํ…Œ์ด๋ธ” ๋ ˆ์ด์•„์›ƒ์„ ์ด์šฉํ•œ ๊ณ„์‚ฐ๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 12. 4.

โ—โ—ํ…Œ์ด๋ธ” ๋ ˆ์ด์•„์›ƒ์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๊ธฐ ์•ฑ ๋งŒ๋“ค๊ธฐโ—โ—

 

 

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
<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"
    tools:context=".MainActivity">
 
    <TableRow>
        <EditText
            android:id="@+id/Edit1"
            android:layout_span="5"
            android:hint="์ˆซ์ž1 ์ž…๋ ฅ"/>
    </TableRow>
 
    <TableRow>
        <EditText
            android:id="@+id/Edit2"
            android:layout_span="5"
            android:hint="์ˆซ์ž2 ์ž…๋ ฅ"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnNum0"
            android:text="0"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum1"
            android:text="1"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum2"
            android:text="2"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum3"
            android:text="3"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum4"
            android:text="4"
            android:layout_weight="1"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnNum5"
            android:text="5"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum6"
            android:text="6"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum7"
            android:text="7"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum8"
            android:text="8"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/BtnNum9"
            android:text="9"
            android:layout_weight="1"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnAdd"
            android:layout_margin="5dp"
            android:layout_span="5"
            android:text="๋”ํ•˜๊ธฐ"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnSub"
            android:layout_margin="5dp"
            android:layout_span="5"
            android:text="๋นผ๊ธฐ"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnMul"
            android:layout_margin="5dp"
            android:layout_span="5"
            android:text="๊ณฑํ•˜๊ธฐ"/>
    </TableRow>
 
    <TableRow>
        <Button
            android:id="@+id/BtnDiv"
            android:layout_margin="5dp"
            android:layout_span="5"
            android:text="๋‚˜๋ˆ„๊ธฐ"/>
    </TableRow>
 
    <TableRow>
        <TextView
            android:id="@+id/TextResult"
            android:layout_margin="5dp"
            android:layout_span="5"
            android:text="๊ณ„์‚ฐ๊ฒฐ๊ณผ : "
            android:textColor="#FF0000"
            android:textSize="20dp"/>
    </TableRow>
 
</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
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;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
    EditText edit1, edit2;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView textResult;
    String num1, num2;
    Integer result;
    Button[] numButtons = new Button[10];
    Integer[] numBtnIDs = {R.id.BtnNum0,R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3,
        R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6, R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9};
    int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        setTitle("ํ…Œ์ด๋ธ”๋ ˆ์ด์•„์›ƒ ๊ณ„์‚ฐ๊ธฐ");
 
        edit1 = (EditText) findViewById(R.id.Edit1);
        edit2 = (EditText) findViewById(R.id.Edit2);
        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) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                result = Integer.parseInt(num1) + Integer.parseInt(num2);
                textResult.setText("๊ณ„์‚ฐ ๊ฒฐ๊ณผ : " + result.toString());
                return false;
            }
        });
 
        btnSub.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                result = Integer.parseInt(num1) - Integer.parseInt(num2);
                textResult.setText("๊ณ„์‚ฐ ๊ฒฐ๊ณผ : " + result.toString());
                return false;
            }
        });
 
        btnMul.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                result = Integer.parseInt(num1) * Integer.parseInt(num2);
                textResult.setText("๊ณ„์‚ฐ ๊ฒฐ๊ณผ : " + result.toString());
                return false;
            }
        });
 
        btnDiv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                num1 = edit1.getText().toString();
                num2 = edit2.getText().toString();
                result = Integer.parseInt(num1) / Integer.parseInt(num2);
                textResult.setText("๊ณ„์‚ฐ ๊ฒฐ๊ณผ : " + result.toString());
                return false;
            }
        });
        for (i=0;i<numBtnIDs.length;i++){
            numButtons[i] = (Button) findViewById(numBtnIDs[i]);
        }
        for (i=0;i<numBtnIDs.length;i++) {
            final int index;
            index = i;
 
            numButtons[index].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (edit1.isFocused() == true) {
                        num1 = edit1.getText().toString()
                                + numButtons[index].getText().toString();
                        edit1.setText(num1);
                    } else if (edit2.isFocused() == true) {
                        num2 = edit2.getText().toString()
                                + numButtons[index].getText().toString();
                        edit2.setText(num2);
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "๋จผ์ € ์—๋””ํŠธํ…์ŠคํŠธ๋ฅผ ์„ ํƒํ•˜์„ธ์š”", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}
cs

 

 

 

 

์‹คํ–‰๊ฒฐ๊ณผ : 

์•ฑ ์‹คํ–‰ ํ›„ ์ฒซ ํ™”๋ฉด

 

 

 

๋”ํ•˜๊ธฐ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ

 

 

๋นผ๊ธฐ ๊ฒฐ๊ณผ

 

 

๊ณฑํ•˜๊ธฐ ๊ฒฐ๊ณผ

 

 

๋‚˜๋ˆ„๊ธฐ  ๊ฒฐ๊ณผ

๋Œ“๊ธ€