본문 바로가기
UI Events Handling

onTouchListener

by 하드락 2022. 10. 4.

onTouchListener 예제 #1

 

< /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">

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"/>
    
    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"
        android:src="@drawable/photo1"
        android:scaleType="fitXY"/>

</LinearLayout>

 

< HelloTestActivity.java >

package com.hardrock.hellotest;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class HelloTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImageView imageview = (ImageView) findViewById(R.id.ImageView01);

        imageview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                switch ( event.getAction() ) {
                    case MotionEvent.ACTION_DOWN:
                        String strResult =
                            String.format("Touch Position : X = %d, Y = %d",
                            (int)event.getX(), (int)event.getY());
                        TextView tv = (TextView) findViewById(R.id.TextView01);
                        tv.setText(strResult);
            	      break; 

                    case MotionEvent.ACTION_UP:
                        break;

                    case MotionEvent.ACTION_MOVE:
                        break;

                }
                return true;

            }
        });

    }
}

 

< RUN >

 

'UI Events Handling' 카테고리의 다른 글

onClick() / onLongClick() / onTouch()  (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

댓글