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 |
AVD๋ฅผ ํตํด ์ดํ์ ์คํ์ํค๊ณ ๋ฒํผ์ ๋๋ ์ ๋ ์ ๋๋ก ์๋ํ๋ ๊ฒ์ ํ์ธ ํ ์ ์์๋ค.
์ผ๋ฐ์ ์ผ๋ก ์๋๋ก์ด๋ ์คํ๋์ค๋ฅผ ์ด์ฉํ ์์ ฏ ์ด๋ฒคํธ ์ฝ๋ฉ ์์๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฝํ ์ ์๋ค.
โ ์์ ฏ ๋ณ์ ์ ์ธ
Button button1;
↓
โก ๋ณ์์ activity_main.xml ํ์ผ์ ์์ ฏ id ๋์
button1 = (Button) findViewById(R.id.button1);
↓
โข ์ด๋ฒคํธ ๋ฐ์ ์ ๋์ํ๋ ํด๋์ค๋ฅผ ์์ ฏ์ ์ ์
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//๋ฒํผ ๋๋ ์ ๋ ์๋ํ ์ฝ๋๋ฅผ ์ด๋ถ๋ถ์ ์ฝ์
}
});
๋๋ถ๋ถ์ view ์์ ฏ๋ค์ ์ด๋ฐ ํ์์ผ๋ก ๋ณ์์ ์ธํ๊ณ ์ฝ๋ฉํ์ฌ ์๋ํ๊ฒ ๋๋ค.
๋๊ธ