#13 안드로이드 스튜디오로 안드로이드 앱 만들기 - 사용자 이벤트 처리 : 델리게이션 이벤트 모델(Delegation Event Model)

2018. 1. 30. 10:54안드로이드

반응형

#8 안드로이드 스튜디오로 안드로이드 앱 만들기 - 레이아웃(Layout)을 활용한 다양한 뷰(View) 배치 : RelativeLayout편

#9 안드로이드 스튜디오로 안드로이드 앱 만들기 - 레이아웃(Layout)을 활용한 다양한 뷰(View) 배치 : 탭 화면 구현 - TabHost

#10 안드로이드 스튜디오로 안드로이드 앱 만들기 - 레이아웃(Layout)을 활용한 다양한 뷰(View) 배치 : ConstraintLayout

#11 안드로이드 스튜디오로 안드로이드 앱 만들기 - 다양한 사용자 알림 효과 : 진동과 소리

#12 안드로이드 스튜디오로 안드로이드 앱 만들기 - 다양한 사용자 알림 효과 : 토스트(Toast), 다이얼로그(Dialog)




스마트폰 화면에서 발생하는 사용자 이벤트는 크게 두 가지 모델로 나누어 볼 수 있다. 델리게이션 이벤트 모델(Delegation Event Model)과 하이어라키 이벤트 모델(Hierarchy Event Model)이 있다.


■ 델리게이션 이벤트 모델(Delegation Event Model) : View에서 발생하는 이벤트 처리, OnClick 등등

■ 하이어라키 이벤트 모델(Hierarchy Event Model) : Activity에서 발생하는 사용자의 터치나 키 이벤트




델리게이션 이벤트 모델

델리게이션 이벤트 모델은 이벤트 소스(Event Source)와 이벤트 핸들러(Event Handler)를 리스너(Listener)로 연결하여 처리하는 구조이다.


■ 이벤트 소스(Event Source) : 이벤트가 발생한 View 객체

■ 핸들러(Event Handler) : 이벤트 처리 내용을 가지는 객체

■ 리스너(Listener) : 이벤트 소스와 이벤트 핸들러를 연결하는 작업


1
labelTextVeiw.setOnClickListener(new MyEventHandler());
cs

안드로이드 프로그램을 작성하다 보면 "setOnXXXListener( )"와 같은 구문을 많이 보게 된다. 이 부분이 이벤트 소스와 이벤트 핸들러를 리스너로 연결하는 부분이다. 코드를 해석하자면 labelTextVeiw 객체에서 ClickEvent가 발생하면 MyEventHandler라는 클래스 객체를 실행하여 이벤트를 처리하라는 의미이다.


이때 이벤트 핸들러로 지정한 MyEventHandler는  개발자가 만든 클래스인데, 이벤트 핸들러 클래스는 꼭 지정된 인터페이스를 구현해야 한다.

1
2
3
4
5
6
7
class MyEventHandler implements View.OnClickListener{
 
   @Override
   public void onClick(View view) {
            
   }
}
cs





그럼 백문의 불여일타. 간단하게 만들어보자.

Step 1_ 모듈 생성

모듈 이름을 "Event"로 지정하여 하나 새로 만든다.


Step 2_ drawable 폴더에 이미지 파일 추가

"bell.png"라는 이름으로 drawable 폴더에 이미지 파일을 하나 추가 한다.



Step 3_ 레이아웃 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#283593">
 
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3b479d"
        android:paddingBottom="16dp"
        >
 
        <TextView
            android:id="@+id/ampm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="오전"
            android:textColor="#FFFFFF"
            android:layout_marginTop="56dp"
            android:layout_marginLeft="16dp"/>
 
        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5:40"
            android:textColor="#FFFFFF"
            android:textSize="40dp"
            android:layout_toRightOf="@id/ampm"
            android:layout_marginLeft="16dp"
            android:layout_alignBaseline="@id/ampm"/>
 
 
        <Switch
            android:id="@+id/onOff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@id/time"
            android:buttonTint="#FF0000"/>
 
        <CheckBox
            android:id="@+id/repeatCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="반복"
            android:textColor="#FFFFFF"
            android:layout_below="@id/ampm"
            android:layout_alignLeft="@id/ampm"
            android:layout_marginTop="16dp"
            android:buttonTint="#FFFFFF"/>
 
        <ImageView
            android:id="@+id/bell"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bell"
            android:layout_below="@id/repeatCheck"
            android:layout_alignLeft="@id/ampm"
            android:layout_marginTop="16dp"/>
        <TextView
            android:id="@+id/bell_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Helium"
            android:textColor="#FFFFFF"
            android:layout_toRightOf="@id/bell"
            android:layout_alignBottom="@id/bell"/>
        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="라벨"
            android:textColor="#FFFFFF"
            android:layout_below="@id/bell_name"
            android:layout_alignLeft="@id/ampm"
            android:layout_marginTop="16dp"/>
        <CheckBox
            android:id="@+id/vibrate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="진동"
            android:textColor="#FFFFFF"
            android:layout_alignParentRight="true"
            android:layout_alignBaseline="@id/bell_name"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:buttonTint="#FFFFFF"/>
    </RelativeLayout>
</LinearLayout>
 
cs


Step 4_ 자바 코드 작성

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
package com.example.event;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{
 
    TextView bellTextView;
    TextView labelTextVeiw;
    CheckBox repeatCheckView;
    CheckBox vibrateCheckView;
    Switch switchView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bellTextView = (TextView)findViewById(R.id.bell_name);
        labelTextVeiw=(TextView)findViewById(R.id.label);
        repeatCheckView=(CheckBox)findViewById(R.id.repeatCheck);
        vibrateCheckView=(CheckBox)findViewById(R.id.vibrate);
        switchView=(Switch)findViewById(R.id.onOff);
 
        bellTextView.setOnClickListener(this);
        labelTextVeiw.setOnClickListener(this);
 
        repeatCheckView.setOnCheckedChangeListener(this);
        vibrateCheckView.setOnCheckedChangeListener(this);
        switchView.setOnCheckedChangeListener(this);
 
 
    }
 
 
    private void showToast(String message){
        Toast toast=Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.show();
    }
 
 
    @Override
    public void onClick(View v) {
        if(v==bellTextView){
            showToast("벨 텍스트 클릭...");
 
        }else if(v==labelTextVeiw){
            showToast("라벨 텍스트 클릭...");
        }
    }
 
 
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isCheck) {
        if(buttonView==repeatCheckView){
            showToast("repeat checkbox is "+isCheck);
 
        }else if(buttonView==vibrateCheckView){
            showToast("vibrate checkbox is "+isCheck);
 
        }else if(buttonView==switchView){
            showToast("switch is "+isCheck);
 
        }
 
    }
}
 
cs



Step 5_ 완성

bellTextView, labelTextView, repeatCheckView, vibrateCheckView, switchView 를 클릭하면 밑에 토스트(Toast)가 잘 뜰 것이다.



반응형