이벤트 처리하기 Do it 예제
XML소스
<?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" >
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="248dp"
android:background="#2196F3" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="259dp"
android:background="#FF9800" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</LinearLayout>
JAVA소스
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.gesture.GestureStroke;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import static java.sql.DriverManager.println;
public class MainActivity extends AppCompatActivity {
TextView textView;
GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView =findViewById(R.id.textView);
View view=findViewById(R.id.textView);
view.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent){
int action = motionEvent.getAction();
float curx=motionEvent.getX();
float cury=motionEvent.getY();
if(action==MotionEvent.ACTION_DOWN){
show("손가락눌림"+curx+","+cury);
}else if(action==MotionEvent.ACTION_MOVE){
show("손가락 움직임"+curx+","+cury);
}else if(action==MotionEvent.ACTION_UP){
show("손가락 움직임"+curx+","+cury);
}
return true;
}
});
detector =new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent motionEvent) {
show("onDown()호출됨");
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
show("onShowPress()호출됨");
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
show("onSingleTapUp()호출됨");
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
show("onScroll()호출됨"+v+","+v1);
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
show("onLongPress()호출됨");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
show("onFling()호출됨"+v+","+v1);
return true;
}
});
View view2=findViewById(R.id.view2);
view2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
detector.onTouchEvent(motionEvent);
return true;
}
});
}
public void show(String s) {
textView.append(s+"/n");
}
}