개발모음집

[안드로이드 프로그래밍] Day11 본문

Android

[안드로이드 프로그래밍] Day11

void 2016. 6. 8. 14:32


이벤트처리



리스너 사용


MyEvent의 MainActivity.java

package com.example.tristan91.myevent;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView textview;
GestureDetector detector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textView);


Button button = (Button) findViewById(R.id.button); // setContentView로 인플래이션을 했기에 id를 가져올 수 있다.

/*
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textview.setText("버튼이 클릭되었습니다");
}
});
*/
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
textview.setText("손가락이 눌렸음");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
textview.setText("손가락이 떼짐");
}
return true; // 정상적인 실행을 하였다.
}
});
detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textview.setText("onScroll() 호출됨: " + distanceX + "," + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
textview.setText("onFling() 호출됨: " + velocityX + "," + velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (detector != null) {
return detector.onTouchEvent(event);
} else {
return super.onTouchEvent(event);
}
}

@Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "onBackPressed() 호출됨", Toast.LENGTH_LONG).show();
return;
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
Toast.makeText(getApplicationContext(), "가로방향으로 됨", Toast.LENGTH_LONG).show();
}else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(getApplicationContext(), "가로방향으로 됨", Toast.LENGTH_LONG).show();
}
super.onConfigurationChanged(newConfig);
}// 매니페이스트에서 설정했던 상태가 바뀌게 되면 이 쪽으로 정보가 날라옴
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tristan91.myevent">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:configChanges="orientation|screenSize|keyboardHidden"// 기기의 변경정보, 자바소스에 값을 넘겨줌
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

패드만들기

MyEvent의 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tristan91.myevent.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시작"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="결과"
android:id="@+id/textView"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="43dp"
android:textSize="30dp" />

<com.example.tristan91.myevent.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:background="#ffeeccff"/>
</RelativeLayout>

<!--
자바소스를 추가하는 방법
1. xml에서 태그로 선언 (패키지명까지 써줘야함)
2. 자바소스에서 객체 생성후 addView로 추가가능
-->

MyEvent의 MyView.java

package com.example.tristan91.myevent;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {

public MyView(Context context) {
super(context);
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}

//리스너는 위젯밖에서 선언해서 동작, 위젯안에서 사용하는 건


@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("MyView", "손가락이 눌렸음");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("MyView", "손가락이 떼짐");
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("MyView", "손가락이 움직인다");
}
return true;
}
}