开发者

How to make the app recognise the screen was touched?

I am currently testing things for m开发者_如何学Cy app and I seem to be having trouble doing some things, when the screen was touched how would i make my app recognise it?


I will give you this sample code to get you started:

public boolean onTouchEvent(MotionEvent e, MapView mv){
        int i = e.getAction();

        switch(i){

        case MotionEvent.ACTION_DOWN:
            //When your finger touches the screen

            break;

        case MotionEvent.ACTION_UP:
            //When your finger stop touching the screen

            break;

        case MotionEvent.ACTION_MOVE:
            //When your finger moves around the screen

            break;
        }

        return false;
    }


As explained before you have to use the method onTouchEvent. Inside it you can get the touch event that lets you know the exact position that has been touched in order to perform different actions. Here is a very simple example:

public boolean onTouchEvent(MotionEvent event) 
{

    if (event.getAction() != MotionEvent.ACTION_DOWN)
    {
        return super.onTouchEvent(event); 
    }        

    int keyX = (int) event.getx();//Returns x coord pulsed

    int keyY = (int) event.gety();//Returns y coord pulsed

    //Do something with data...

    return true;
}

I think that with all this you can start playing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜