프로그래밍/안드로이드

안드로이드 Touch 이벤트 처리하기

가카리 2013. 5. 3. 14:07
반응형

안드로이드 터치 이벤트가 발생시 처리하는 방법

 

package com.example.touchex;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

 

import android.view.MotionEvent;

import android.view.View;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

           @Override

           protected void onCreate(Bundle savedInstanceState) {

                     super.onCreate(savedInstanceState);

                    

                     View v = new MyView(this);

                     setContentView(v);

           }

          

           protected class MyView extends View{

 

                     public MyView(Context context) {

                                super(context);

                                // TODO Auto-generated constructor stub

                     }

 

                    

                     //이 메소드를 오버라이드해서 아래 하고싶은 것을 하면 됩니다.

                     @Override

                     public boolean onTouchEvent(MotionEvent event) {

                                // TODO Auto-generated method stub

                    

                                if(event.getAction()==MotionEvent.ACTION_DOWN){

                                          //여기서는 터치가 되면

                                          toast();//토스트 메시지가 출력됩니다.

                                          return true;

                                }

                               

                                return false;

                    

                     }

                    

           }

          

           public void toast(){

                    

                     String str = "it touched";

                     Toast.makeText(MainActivity.this,str, Toast.LENGTH_SHORT).show();

                    

           }

 

}

 

 

 

반응형