Android studio programming

(22.11.19)Android Studio ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์šฐ๋ฆฌ์ง‘ ๊ณ ์–‘์ด ์‚ฌ์ง„ ์ถœ๋ ฅํ•˜๋Š” ์•ฑ ๋งŒ๋“ค๊ธฐ

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

์›ํ•˜๋Š” ๊ณ ์–‘์ด ์‚ฌ์ง„์„ ์ถœ๋ ฅํ•˜๋Š” ์•ฑ ๋งŒ๋“ค๊ธฐ

'์‹œ์ž‘ํ•จ'์— ์ฒดํฌํ•˜๋ฉด ์›ํ•˜๋Š” ์‚ฌ์ง„ ๋ฒˆํ˜ธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๋ผ๋Š” ๋‚ด์šฉ์ด ๋‚˜์˜ค๊ณ , ์„ ํƒ ํ›„์— <์„ ํƒ์™„๋ฃŒ> ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ๊ณ ์–‘์ด์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

์‚ฌ์ง„์„ ๋ณต์‚ฌ ํ•˜์—ฌ [res]-[drawable]์— ๋ถ™์–ด ๋„ฃ๊ธฐ ํ•ด์ค€๋‹ค.


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
<?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">
 
    <TextView
        android:id="@+id/Text1"
        android:text="์„ ํƒ์„ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๊นŒ?"
        android:layout_marginLeft="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/chkAgree"
        android:text="์‹œ์ž‘ํ•จ"
        android:layout_marginLeft="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 
    <TextView android:id="@+id/Text2"
        android:text="๋‹น์‹ ์˜ ๋‹ฌ์ด ๋ฒ ์ŠคํŠธ ํ”ฝ์€?"
        android:visibility="invisible"
        android:layout_marginLeft="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RadioGroup
        android:id="@+id/Rgroup1"
        android:visibility="invisible"
        android:layout_marginLeft="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/RdoDal1"
            android:text="๋‹ฌ์ด์‚ฌ์ง„1"
            android:layout_marginLeft="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/RdoDal2"
            android:text="๋‹ฌ์ด์‚ฌ์ง„2"
            android:layout_marginLeft="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/RdoDal3"
            android:text="๋‹ฌ์ด์‚ฌ์ง„3"
            android:layout_marginLeft="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </RadioGroup>
 
    <Button
        android:id="@+id/BtnOk"
        android:text="์„ ํƒ์™„๋ฃŒ"
        android:visibility="invisible"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/ImgDal"
        android:visibility="invisible"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
 
 
</LinearLayout>
cs

๋งจ์œ„ TextView ์™€ CheckBox๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์œ„์ ฏ์€ visibility ์†์„ฑ์„ invisible๋กœ ์ง€์ •ํ•œ๋‹ค.

RadioGroup์—์„œ visibility ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด ๊ทธ ์•ˆ์˜ RadioButton๋“ค์˜ ์†์„ฑ์„ ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๋ผ๋””์˜ค ๊ทธ๋ฃน์œผ๋กœ ๋ผ๋””์˜ค ๋ฒ„ํŠผ 1,2,3์„ ๋ฌถ์–ด์„œ ํ•ด๋‹น ๋ผ๋””์˜ค ๊ทธ๋ฃน ์‚ฌ์ด์—์„œ๋งŒ ์ค‘๋ณต์ด ๋˜์ง€ ์•Š๊ฒŒ ํ•ด์ค€๋‹ค.

์„ ํƒํ•จ์„ ์„ ํƒํ•ด์•ผ ์‚ฌ์ง„์„ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ ํƒํ•จ์ด๋ผ๋Š” ์ฒดํฌ๋ฐ•์Šค๋„ ๋งŒ๋“ค์–ด ์ค€๋‹ค.


 

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
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
    TextView text1,text2;
    CheckBox chkAgree;
    RadioGroup rGroup1;
    RadioButton rdoDal1, rdoDal2, rdoDal3;
    Button btnOk;
    ImageView imgDal;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("๋‹ฌ์ด ์‚ฌ์ง„ ๋ณด๊ธฐ");
 
        text1= (TextView) findViewById(R.id.Text1);
        chkAgree= (CheckBox) findViewById(R.id.chkAgree);
 
        text2= (TextView) findViewById(R.id.Text2);
        rGroup1=(RadioGroup) findViewById(R.id.Rgroup1);
        rdoDal1=(RadioButton) findViewById(R.id.RdoDal1);
        rdoDal2=(RadioButton) findViewById(R.id.RdoDal2);
        rdoDal3=(RadioButton) findViewById(R.id.RdoDal3);
 
        btnOk= (Button) findViewById(R.id.BtnOk);
        imgDal= (ImageView) findViewById(R.id.ImgDal);
 
        chkAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(chkAgree.isChecked()==true){
                    text2.setVisibility(android.view.View.VISIBLE);
                    rGroup1.setVisibility(android.view.View.VISIBLE);
                    btnOk.setVisibility(android.view.View.VISIBLE);
                    imgDal.setVisibility(android.view.View.VISIBLE);
                }else{
                    text2.setVisibility(android.view.View.INVISIBLE);
                    rGroup1.setVisibility(android.view.View.INVISIBLE);
                    btnOk.setVisibility(android.view.View.INVISIBLE);
                    imgDal.setVisibility(android.view.View.INVISIBLE);
                }
            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                switch (rGroup1.getCheckedRadioButtonId()){
                    case R.id.RdoDal1:
                        imgDal.setImageResource(R.drawable.dal1);
                        break;
                    case R.id.RdoDal2:
                        imgDal.setImageResource(R.drawable.dal2);
                        break;
                    case R.id.RdoDal3:
                        imgDal.setImageResource(R.drawable.dal3);
                        break;
                    default:
                        Toast.makeText(getApplicationContext(),
                                "๋‹ฌ์ด ์‚ฌ์ง„๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜์„ธ์š”", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
cs

 

14ํ–‰~19ํ–‰ : ์œ„ ํด๋ž˜์Šค์—์„œ ์“ธ ์ „์—ญ๋ณ€์ˆ˜๋“ค์„ ์„ ์–ธํ•ด ์ค€๋‹ค.

27ํ–‰~37ํ–‰ : ๊ฐ ์œ„์ ฏ์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•œ๋‹ค. XML ์—์„œ ์ •ํ•ด์ค€ id๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

39ํ–‰ :  '์‹œ์ž‘ํ•จ'  ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌ/์–ธ์ฒดํฌํ•  ๋•Œ๋งˆ๋‹ค ๋™์ž‘ํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

์ปดํŒŒ์šด๋“œ ๋ฒ„ํŠผ์ด๊ธฐ ๋•Œ๋ฌธ์— View.OnCheckedChangeListener()๊ฐ€ ์•„๋‹ˆ๋ผ CompoundButton.OnCheckedChangeListener() ๋ฆฌ์Šค๋„ˆ์ด๋‹ค.

42ํ–‰ : ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์ฒดํฌ ๋˜์–ด ์žˆ๋Š” ์กฐ๊ฑด์ผ๋•Œ๋ฅผ ๋งํ•œ๋‹ค.

43ํ–‰~46ํ–‰ : ์ˆจ๊ฒจ์ ธ ์žˆ๋˜ ๋ชจ๋“  ์œ„์ ฏ์„ ๋ณด์ด๋„๋ก ํ•œ๋‹ค.

47ํ–‰ ~ 51ํ–‰ :  ์ฒดํฌ๊ฐ€๊บผ์ง€๋ฉด ์œ„์ ฏ๋“ค์„ ์ˆจ๊ธด๋‹ค.

 

55ํ–‰ : ์„ ํƒ ์™„๋ฃŒ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋™์ž‘ํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

59ํ–‰~70ํ–‰ : switch()~ case ๋ฌธ์œผ๋กœ ๋‹ค์ค‘ ๋ถ„๊ธฐํ•œ๋‹ค. getCheckedRadioButtonId()๋Š” ํ˜„์žฌ ๋ผ๋””์˜ค ๊ทธ๋ฃน์—์„œ ์„ ํƒ๋œ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์˜ ์•„์ด๋””๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ฐ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์— ๋งž๋Š” ์‚ฌ์ง„์„ ๋ณด์—ฌ์ค€๋‹ค.

68~70ํ–‰ : ๋ผ๋””์˜ค๋ฒ„ํŠผ์ค‘์—์„œ ์•„๋ฌด๊ฒƒ๋„ ์„ ํƒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.


 

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

์ฒ˜์Œ์— ์—๋ฎฌ๋ ˆ์ดํ„ฐ๋Š” ๋Œ์•„๊ฐ€๋Š”๋ฐ ์–ดํ”Œ์ด ์‹คํ–‰๋˜์ง€ ์•Š์•„์„œ ๋ช‡์‹œ๊ฐ„์„ ํ•ด๊ฒฐํ•˜๋Š๋ผ ๊ณ ์ƒํ–ˆ๋‹ค..........

๋ฌธ์ œ์˜ ์›์ธ์€ AVD์„ค์น˜ํ•  ๋•Œ  ์ €์žฅ์†Œ๊ฐ€ 800mb๊ฐ€ ๋””ํดํŠธ๋กœ ์ •ํ•ด์ ธ ์žˆ์–ด ์šฉ๋Ÿ‰์ด ๋ถ€์กฑํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์ด์—ˆ๋‹ค.

 

์–ดํ”Œ ์‹œ์ž‘ํ–ˆ์„ ์‹œ ์ดˆ๊ธฐ ํ™”๋ฉด

 

'์‹œ์ž‘ํ•จ' ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ–ˆ์„ ์‹œ ๋‚˜์˜จ ํ™”๋ฉด
๋‹ฌ์ด ์‚ฌ์ง„ 2๋ฒˆ์„ ๋จผ์ € ์„ ํƒํ•ด์ฃผ์—ˆ๋‹ค
์„ ํƒ ์™„๋ฃŒ ๋ˆ„๋ฅธ ํ›„ ํ™”๋ฉด
๋‹ฌ์ด ์‚ฌ์ง„3๋ฒˆ์„ ๋ˆŒ์—ˆ์„ ์‹œ
๋‹ฌ์ด ์‚ฌ์ง„1๋ฒˆ์„ ๋ˆŒ๋ €์„ ์‹œ

 

๋Œ“๊ธ€