< /res/layout/main.xml >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Basic Button"/>
</LinearLayout>
< HelloTestActivity.java >
package com.hardrock.hellotest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class HelloTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("MY_TAG", "onClick()");
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Log.d("MY_TAG", "onLongClick()");
return true;
}
});
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
Log.e("MY_TAG", "onTouch() -> ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e("MY_TAG", "onTouch() -> ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e("MY_TAG", "onTouch() -> ACTION_UP");
break;
}
return true;
}
});
}
}
< RUN >
| 1. onTouch() 에서 return true 인 경우 onClick() 및 onLongClick()이 동작하지 않는다. onTouch()에서 true를 반환해 버리면, onClick() 및 onLongClick()에 이벤트가 전달되지 않는다. 2. onTouch() 에서 return false 인 경우 onClick() 및 onLongClick()이 동작한다. <<< Short Press >>> onTouch() -> ACTION_DOWN onTouch() -> ACTION_UP onClick() <<< Long Press >>> onTouch() -> ACTION_DOWN onLongClick() onTouch() -> ACTION_UP |
'UI Events Handling' 카테고리의 다른 글
| onTouchListener (0) | 2022.10.04 |
|---|---|
| onFocusChangeListener (0) | 2022.10.04 |
| onKeyListener (0) | 2022.10.04 |
| onClickListener (0) | 2022.10.03 |
| UI Events Handling (0) | 2022.10.03 |
댓글