Android studio programming

(22.10.30.)Android ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ๋ณธ ์œ„์ ฏ-ํ…์ŠคํŠธ๋ทฐ, ๋ฒ„ํŠผ, ์—๋””ํŠธํ…์ŠคํŠธ ์‹ค์Šต

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

๊ธฐ๋ณธ์ ์ธ ์œ„์ ฏ์ธ ํ…์ŠคํŠธ๋ทฐ, ์—๋””ํŠธํ…์ŠคํŠธ, ๋ฒ„ํŠผ์„ ์ด์šฉํ•˜์—ฌ ์ •์ˆ˜ ๊ณ„์‚ฐ๊ธฐ ์•ฑ ๋งŒ๋“ค์–ด ๋ณด๊ธฐ

 

XML ์ฝ”๋“œ(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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
 
    <EditText
        android:id="@+id/Edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="์ˆซ์ž1" />
 
    <EditText
        android:id="@+id/Edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="์ˆซ์ž2" />
 
    <Button
        android:id="@+id/BtnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋”ํ•˜๊ธฐ"/>
 
    <Button
        android:id="@+id/BtnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋นผ๊ธฐ"/>
 
    <Button
        android:id="@+id/BtnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๊ณฑํ•˜๊ธฐ"/>
 
    <Button
        android:id="@+id/BtnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋‚˜๋ˆ„๊ธฐ"/>
 
 
    <TextView
        android:id="@+id/TextResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textColor="#FF0000"
        android:layout_margin="10dp"
        android:text="๊ณ„์‚ฐ ๊ฒฐ๊ณผ : " />
 
</LinearLayout>
cs

android:id๋กœ ์ž๋ฐ” ์ฝ”๋“œ์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ id๋ฅผ ์ •ํ•ด์ฃผ๊ณ 

android:layout_width์™€android:layout_height์œผ๋กœ ์œ„์ ฏ์˜ ํฌ๊ธฐ(๋„ˆ๋น„์™€ ๋†’์ด)๋ฅผ ์ง€์ •ํ•ด ์ค€๋‹ค.

android:textSize๋กœ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ณ 

android:textColor ํ…์ŠคํŠธ ๊ธ€์ž ์ƒ‰์„ ์ •ํ•ด์ค€๋‹ค. ๊ฒฐ๊ณผ ๊ฐ’์ด ์ž˜ ๋ณด์ด๊ฒŒ๋” RGB ๊ฐ’์ค‘ ๋นจ๊ฐ•์ƒ‰์œผ๋กœ ์ •ํ•ด์ฃผ์—ˆ๋‹ค.

android:layout_margin="10dp"์œผ๋กœ ์œ„์ ฏ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ์กฐ์ •ํ•ด์ฃผ์—ˆ๋‹ค.

android:text์œผ๋กœ ์œ„์ ฏ ์•ˆ์— ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ์ •ํ•ด์ฃผ์—ˆ๋‹ค.

<LineanrLayout>์—์„œ android:orientation์„ vertical๋กœ ์ฃผ์–ด ์œ„์ ฏ๋“ค์ด ์„ธ๋กœ๋กœ ๋‚˜์—ด๋˜๊ฒŒ ํ–ˆ๋‹ค.

<LinearLayout/>,<EditText/> ์–‘์‹
<Button/>์–‘์‹
<Button/>,<TextView/> ์–‘์‹

Java ์ฝ”๋“œ(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
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;
 
public class MainActivity extends AppCompatActivity {
    EditText edit1, edit2;
    Button btnAdd, btnSub, btnMul, btnDiv;
    TextView textResult;
    String num1, num2;
    Integer result;
    @Override
    public 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 arg0, MotionEvent arg1) {
                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 arg0, MotionEvent arg1) {
                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 arg0, MotionEvent arg1) {
                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 arg0, MotionEvent arg1) {
                num1= edit1.getText().toString();
                num2= edit2.getText().toString();
                result= Integer.parseInt(num1) / Integer.parseInt(num2);
                textResult.setText("๊ณ„์‚ฐ ๊ฒฐ๊ณผ : "+ result.toString());
                return false;
            }
        });
    }
}
cs

13์—ด~17์—ด

activity_main.xml์˜ 7๊ฐœ ์œ„์ ฏ์— ๋Œ€์‘ํ•  ์œ„์ ฏ ๋ณ€์ˆ˜ 7๊ฐœ, ์ž…๋ ฅ๋  2๊ฐœ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ 2๊ฐœ,  ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์ •์ˆ˜ ๋ณ€์ˆ˜ 1๊ฐœ

๋ชจ๋“  ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ๋‹ค.

 

23์—ด~29์—ด

activity_main.xml ์ฝ”๋“œ์—์„œ ์ƒ์„ฑํ•œ ์œ„์ ฏ์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•œ๋‹ค.

 

31์—ด

๋”ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํ„ฐ์น˜ํ–ˆ์„ ๋•Œ์˜ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

34,35์—ด

์—๋””ํŠธ ํ…์ŠคํŠธ์— ์ž…๋ ฅ๋œ ๊ฐ’์„ num1, num2 ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•ด์ค€๋‹ค.

 

36์—ด

Integer.parseInt() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ num1, num2๋ฅผ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ๋‘ ๊ฐ’์„ ๋”ํ•œ ํ›„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

 

37์—ด

์ •์ˆ˜ํ˜• ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ setText() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ทฐ์— ๋Œ€์ž…ํ•œ๋‹ค.

 

๋นผ๊ธฐ, ๊ณฑํ•˜๊ธฐ, ๋‚˜๋ˆ„๊ธฐ๋„ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ•ด์ค€๋‹ค.


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

๋”ํ•˜๊ธฐ ๊ฒฐ๊ณผ
๋นผ๊ธฐ ๊ฒฐ๊ณผ
๊ณฑํ•˜๊ธฐ ๊ฒฐ๊ณผ
๋‚˜๋ˆ„๊ธฐ ๊ฒฐ๊ณผ

๋Œ“๊ธ€