** 7가지 EventHandler **
1. 프로젝트명 : ClickEvent
2. 정의 : EventHandler 종료
3. 소스명 :
1. MainActivity.java
2. ClickHandler.java
3. activity_main.java
4. 안드로이드 버전
- 스튜디오 : v2.2.3
- Minumum SDK : API 19:Android 4.4. (KitKat)
5. 화면
5. 설명
1. 익명 내부 클래스 처리
2. 유명 내부 클래스 처리
3. 유명 내부 클래스
4. 제3클래스로 처리
5. 자신의 클래스로 처리
6. XML에서 핸들러 이벤트 저장
7. 유명 외부 메소드
|
1. MainActivity.java |
|
package com.example.farmer.clickevent;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn1; Button btn2; Button btn3; Button btn4; Button btn5; Button btn6; Button btn7; EditText et01;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn01); btn2 = (Button)findViewById(R.id.btn02); btn3 = (Button)findViewById(R.id.btn03); btn4 = (Button)findViewById(R.id.btn04); btn5 = (Button)findViewById(R.id.btn05); btn6 = (Button)findViewById(R.id.btn06); btn7 = (Button)findViewById(R.id.btn07); et01 = (EditText)findViewById(R.id.editText01);
//1. 익명 내부 클래스 : 클래스 이름이 없다. // 일회성 : 한번만 쓸 때 사용 btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { et01.setText("1. 익명 내부 클래스 처리"); showToast("1. 익명 내부 클래스 처리"); } });
//2. 유명 내부 클래스 View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View view) { et01.setText("2. 유명 내부 클래스 처리"); showToast("2. 유명 내부 클래스 처리"); } }; btn2.setOnClickListener(onClickListener);
//3. 유명 내부 클래스 btn3.setOnClickListener(new InnerHandler());
//4. 제3클래스로 처리 btn4.setOnClickListener(new ClickHandler(MainActivity.this));
//5. 자신의 클래스로 처리 // 이건 [public class MainActivity extends AppCompatActivity implements View.OnClickListener] // 이렇게 함으로써 처리 됨 // 아래 [public void onClick(View view) { ] 와 연결 됨 btn5.setOnClickListener(this);
//7. btn7.setOnClickListener(onClickListener7); }
//3. 유명 내부 클래스 class InnerHandler implements View.OnClickListener{ public void onClick(View view) { et01.setText("3. 유명 내부 클래스 처리"); showToast("3. 유명 내부 클래스 처리"); } }
//5. 자신의 클래스로 처리 // 이건 [public class MainActivity extends AppCompatActivity implements View.OnClickListener] // 이렇게 함으로써 처리 됨 // 위쪽 [btn5.setOnClickListener(this);] 이 script로 연결됨 @Override public void onClick(View view) { et01.setText("5. 자신의 클래스로 처리"); showToast("5. 자신의 클래스로 처리"); }
//6. XML에서 핸들러 이벤트 저장 public void xmlHandlerMethod(View view){ et01.setText("6. XML에서 핸들러 이벤트 저장"); showToast("6. XML에서 핸들러 이벤트 저장"); }
//7. 유명 외부 메소드 private View.OnClickListener onClickListener7 = new View.OnClickListener(){ @Override public void onClick(View view) { et01.setText("7. 유명 외부 메소드"); showToast("7. 유명 외부 메소드"); } };
public void showToast(String msg){ Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } } |
|
2. CllickHandler.java |
|
package com.example.farmer.clickevent;
import android.view.View;
/** * Created by farmer on 2017-01-07. */
public class ClickHandler implements View.OnClickListener { MainActivity activity;
public ClickHandler(MainActivity activity) { this.activity = activity; }
@Override public void onClick(View view) { activity.et01.setText("4. 제3클래스로 처리"); activity.showToast("4. 제3클래스로 처리"); } } |
|
3. activity_main.xml |
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.farmer.clickevent.MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:textSize="30dp" android:text="OnClickEvent" android:id="@+id/textView" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btn01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="버튼 1"/>
<Button android:id="@+id/btn06" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="xmlHandlerMethod" android:text="버튼 6 (xml)"/> </LinearLayout>
<Button android:id="@+id/btn02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼 2"/> <Button android:id="@+id/btn03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼 3"/> <Button android:id="@+id/btn04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼 4"/> <Button android:id="@+id/btn05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼 5"/> <Button android:id="@+id/btn07" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="버튼 7"/>
<EditText android:id="@+id/editText01" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:editable="false" android:hint="hint"/> </LinearLayout> |