Android studio programming

(22.10.10)์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ดํ”Œ ์œ„์ ฏ ์‹ฌํ™” ์‹ค์Šต & ์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ ๊ณต๋ถ€

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

Android Project tree์˜ ๋ฉ”์ธ ์ฝ”๋“œ๋Š” ํŒŒ์ผ ์ด๋ฆ„์ด 'MainActivity.java'๋กœ ๋˜์–ด ์žˆ๋‹ค. ์ด ํŒŒ์ผ์—์„œ๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ (ํ™”๋ฉด)์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด์„œ setContentView(R.layout.activity_main);์ด๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ activity_main์ด ๋ฐ”๋กœ [res] ํด๋”์˜ ํ•˜๋ถ€ ํŠธ๋ฆฌ์— ์žˆ๋Š” [layout] - [activity_main.xml]์ด ๋œ๋‹ค. ์‰ฝ๊ฒŒ R.layout.activity_main์ด ๋ฐ”๋กœ activity_main.xml ํŒŒ์ผ์ด๋‹ค.

Android studio Project์˜ ๊ธฐ๋ณธ ์†Œ์Šค์ธ Java ํŒŒ์ผ ์ด๋ฆ„์€ MainActivity.java๋ฅผ, ๊ธฐ๋ณธ ํ™”๋ฉด์ธ XML ํŒŒ์ผ ์ด๋ฆ„์€ activity_main.xml์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ง€๋‚œ ๋ฒˆ ๋งŒ๋“ค์—ˆ๋˜ BassApp์— ์ถ”๊ฐ€๋กœ 3๊ฐ€์ง€ ๋‹ค๋ฅธ ๋ฒ„ํŠผ์„ ๋„ฃ์–ด ์ข€ ๋” ์‹ฌํ™”๋œ ์–ดํ”Œ์„ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

[ activity_main.xml ]

( [res]-[layout] - [activity_main.xml] )

๋ฒ„ํŠผ์˜ ๋„ˆ๋น„ = layout_width
๋ฒ„ํŠผ์˜ ๋†’์ด = layout_height

android:orientation

์ด๊ฒƒ์€ ์ž์‹ View ์œ„์ ฏ๋“ค์ด ๋‚˜์—ด๋˜๋Š” ๋ฐฉํ–ฅ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์—ฌ๊ธฐ์„œ vertical๋กœ ํ•˜์˜€๊ธฐ์— ์ƒˆ๋กœ๋กœ ๋‚˜์—ด๋œ๋‹ค.
๊ฐ€๋กœ๋กœ ๋‚˜์—ดํ•˜๊ณ  ์‹ถ์„ ๋• horizontal์„ ์“ฐ๋ฉด ๋˜๊ณ  ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ horizontal์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค.

๋ชจ๋“  ๋ทฐ(view) ์™€ ๋ทฐ์—์„œ ์ƒ์†๋œ ๊ฒƒ์€ ์•„์ด๋””๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์•„์ด๋””(id)๋Š” " @+id/์ง€์ • ์•„์ด๋”” " ํ˜•์‹์œผ๋กœ ์“ด๋‹ค. @์€ ๋ฆฌ์†Œ์Šค์—์„œ ์ฐธ์กฐํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๊ณ  @+๋Š” ๋ฆฌ์†Œ์Šค์— ์ถ”๊ฐ€ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ ์ง€์ • ์•„์ด๋””๋Š” button1,2,3,4๋กœ ํ•ด์ฃผ์—ˆ๋‹ค.

text ์†์„ฑ์€ ๋ฒ„ํŠผ์— ์“ฐ์ผ ๊ธ€์ž์ด๋‹ค. ์ง์ ‘ text="๋„ค์ด๋ฒ„ ์—ด๊ธฐ" ๋ผ๊ณ  ์จ๋„ ๋ฌด๋ฐฉํ•˜์ง€๋งŒ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ๋ฌธ์ž์—ด์€ ๋˜๋„๋ก strings.xml์— ๋ชจ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. "@string/๋ฌธ์ž์—ด ์ด๋ฆ„"์€ strings.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
<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="@string/strBtn1" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="@string/strBtn2" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="@string/strBtn3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="@string/strBtn4"/>
 
</LinearLayout>
cs

[ strings.xml ]

[res]-[values] - [strings.xml]

์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด ๋ฌธ์ž์—ด๋“ค ์ •๋ณด๋ฅผ ๋ชจ์•„๋‘” ๊ณณ์ด๋‹ค.
๋ฌธ์ž์—ด ์ด๋ฆ„์— ์ƒ์‘ํ•˜๋Š” ๋‚ด์šฉ์„ ์ ์–ด์ฃผ๋ฉด AVD์„ ๊ตฌ๋™์‹œ์ผœ ์–ดํ”Œ์„ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ๋ฒ„ํŠผ์— ์ƒ์‘ํ•˜๋Š” ๋‚ด์šฉ์ด ์ ํ˜€์žˆ๊ฒŒ ๋œ๋‹ค.

strings.xml ์ฝ”๋“œ

1
2
3
4
5
6
7
<resources>
    <string name="app_name">BaseApp</string>
    <string name="strBtn1">๋„ค์ด๋ฒ„ ์—ด๊ธฐ</string>
    <string name="strBtn2">๊ธด๊ธ‰์ „ํ™” ์—ด๊ธฐ</string>
    <string name="strBtn3">๊ฐค๋Ÿฌ๋ฆฌ ์—ด๊ธฐ</string>
    <string name="strBtn4">๋๋‚ด๊ธฐ</string>
</resources>
cs

 

[ MainActivity.java ]


setContentView()๋Š” ๋ ˆ์ด์•„์›ƒ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•ด์ค€๋‹ค. R.layout.activity_main์€ activity_main.xml ํŒŒ์ผ์„ ์˜๋ฏธํ•œ๋‹ค.

Java ์ฝ”๋“œ์—์„œ activity_main.xml์˜ ๋ฒ„ํŠผ(Button)์— ๋Œ€ํ•ด ์ ‘๊ทผํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฒ„ํŠผ์— ๋Œ€ํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ์œ„์น˜์ธ @Override ๋ฐ”๋กœ ์œ„์— button1,2,3,4๋ฅผ ์ถ”๊ฐ€ ์‹œ์ผœ ์ค€๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ฒ„ํŠผ๊ณผ ๊ด€๋ จ๋œ Class๋ฅผ ์ž„ํฌํŠธ๋ฌธ์— ์ถ”๊ฐ€ ํ•ด์ค€๋‹ค.

๊ทธ ๋‹ค์Œ activity_main.xml์˜ ํŒŒ์ผ์—์„œ ๋งŒ๋“  ๋ฒ„ํŠผ(Button) ๊ฐ์ฒด์— ๋Œ€ํ•ด ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”๋ฐ findViewById() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค. " .setBackgroundColor() " ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฒ„ํŠผ์˜ ์ƒ‰๊น”์„ ์ •ํ•˜๊ณ , " .setTextColor() " ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฒ„ํŠผ ์•ˆ์— ์ ํžˆ๋Š” ๊ธ€์”จ์˜ ์ƒ‰์ƒ์„ ์ •ํ•ด์ค€๋‹ค.

" .setOnClickListener() " ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฒ„ํŠผ์ด ํด๋ฆญํ•˜๋ฉด ์ž‘๋™ํ•˜๋Š” ์ด๋ฒคํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ ํ•ด์ฃผ๋ฉด๋œ๋‹ค. ( ) ์•ˆ์— new view ์ž…๋ ฅํ•˜๋ฉด onClick() ๋ฉ”์†Œ๋“œ๊ฐ€ ์ž๋™ ์ถ”๊ฐ€๋œ๋‹ค. ๋ฒ„ํŠผ์ด ์ž‘๋™ํ•  ๋•Œ ์›ํ•˜๋Š” ๋ชจ๋“  ์ด๋ฒคํŠธ๋“ค์„ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
๋ฒ„ํŠผ2๋Š” ๊ธด๊ธ‰์ „ํ™” ๊ฑธ๊ธฐ, ๋ฒ„ํŠผ3์€ ๊ฐค๋Ÿฌ๋ฆฌ ์—ด๊ธฐ, ๋ฒ„ํŠผ4๋Š” ์–ดํ”Œ ๋๋‚ด๊ธฐ ๊ธฐ๋Šฅ์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

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
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
import java.net.HttpCookie;
 
public class MainActivity extends AppCompatActivity {
 
    Button button1, button2, button3, button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setBackgroundColor(Color.GREEN);
        button1.setTextColor(Color.BLACK);
 
        button2 = (Button) findViewById(R.id.button2);
        button2.setBackgroundColor(Color.BLUE);
        button2.setTextColor(Color.BLACK);
 
        button3 = (Button) findViewById(R.id.button3);
        button3.setBackgroundColor(Color.RED);
        button3.setTextColor(Color.BLACK);
 
        button4 = (Button) findViewById(R.id.button4);
        button4.setBackgroundColor(Color.YELLOW);
        button4.setTextColor(Color.BLACK);
 
 
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
 
                Toast.makeText(getApplicationContext(), "๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์–ด์š”",
                        Toast.LENGTH_SHORT).show();
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
                startActivity(mIntent);
 
 
            }
        });
 
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
 
                Toast.makeText(getApplicationContext(), "๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์–ด์š”",
                        Toast.LENGTH_SHORT).show();
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/911"));
                startActivity(mIntent);
            }
        });
 
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
 
                Toast.makeText(getApplicationContext(), "๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์–ด์š”",
                        Toast.LENGTH_SHORT).show();
                Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/
internal/images/media"
));
                startActivity(mIntent);
            }
        });
 
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
 
                Toast.makeText(getApplicationContext(), "๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์–ด์š”",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}
cs




๋ฒ„ํŠผ 2(๊ธด๊ธ‰์ „ํ™” ์—ด๊ธฐ)๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ
๋ฒ„ํŠผ3 (๊ฐค๋Ÿฌ๋ฆฌ ์—ด๊ธฐ)๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ
๋ฒ„ํŠผ 4(๋๋‚ด๊ธฐ)๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ

AVD๋ฅผ ํ†ตํ•ด ์–ดํ”Œ์„ ์‹คํ–‰์‹œํ‚ค๊ณ  ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋ฅผ ์ด์šฉํ•œ ์œ„์ ฏ ์ด๋ฒคํŠธ ์ฝ”๋”ฉ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

โ‘  ์œ„์ ฏ ๋ณ€์ˆ˜ ์„ ์–ธ

 Button button1;


โ‘ก ๋ณ€์ˆ˜์— activity_main.xml ํŒŒ์ผ์˜ ์œ„์ ฏ id ๋Œ€์ž…

 button1 = (Button) findViewById(R.id.button1);


โ‘ข ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๋™์ž‘ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์œ„์ ฏ์— ์ •์˜

	button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
			//๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ์ž‘๋™ํ•  ์ฝ”๋“œ๋ฅผ ์ด๋ถ€๋ถ„์— ์‚ฝ์ž…
   		}
	});

๋Œ€๋ถ€๋ถ„์˜ view ์œ„์ ฏ๋“ค์€ ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ๋ณ€์ˆ˜์„ ์–ธํ•˜๊ณ  ์ฝ”๋”ฉํ•˜์—ฌ ์ž‘๋™ํ•˜๊ฒŒ ๋œ๋‹ค.

๋Œ“๊ธ€