728x90
반응형
💡OnTouchListener 이벤트
◾ 터치(클릭)할 때마다 Log를 찍는다.
📝MainActivity.java
package com.example.touchstudy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TextView tv = findViewById(R.id.tv);
tv.setOnTouchListener(lis);
}
View.OnTouchListener lis = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.i("jyeon", "onTouch");
return false;
}
};
}
📃onTouchListener 예제 (1)
◾ motionEvent를 getAction()으로 확인할 수 있다.
📝MainActivity.java
package com.example.touchstudy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TextView tv = findViewById(R.id.tv);
tv.setOnTouchListener(lis);
}
View.OnTouchListener lis = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
float x = motionEvent.getX();
float y = motionEvent.getY();
Log.i("jyeon", "onTouch : action" + action + ", x : " + x + ", y : " + y);
return true;
}
};
}
📃onTouchListener 예제 (2)
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/snow"
android:id="@+id/iv"/>
</RelativeLayout>
📝MainActivity.java
package com.example.touchstudy;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String str_action[] = {"DOWN", "UP", "MOVE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ImageView iv = findViewById(R.id.iv);
iv.setOnTouchListener(lis);
}
View.OnTouchListener lis = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction()>=0 && motionEvent.getAction() < str_action.length) {
Log.i("jyeon", "action : " + str_action[motionEvent.getAction()] + "\n"
+ "x, y position : " + motionEvent.getX() + ", " + motionEvent.getY() + "\n"
+ "event time : " + motionEvent.getEventTime() + "\n"
+ "down event time" + motionEvent.getDownTime());
}
return true;
}
};
}
📝Logcat
📃onTouchEvent 예제
📝MainActivity.java
package com.example.touchevent;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintSet;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("jyeon", "onTouchEvent");
return super.onTouchEvent(event);
}
}
📝Logcat
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - View, Layout, Activity (0) | 2022.08.04 |
---|---|
Android Studio - 이벤트 리스너 활용 문제 (0) | 2022.08.04 |
Android Studio - OnClickListener 활용 (0) | 2022.08.03 |
Android Studio - OnClickListener 이벤트 리스너 (0) | 2022.08.03 |
Android Studio - 로또 번호 발생기 (0) | 2022.08.02 |
댓글