J2ME 触摸屏处理

J2ME 触摸处理

 

一, MIDP Canvas 类里面就有处理触摸技术相关的类,直接用就行了,蛮简单。

 

类结构:
java.lang.Object


|_  javax.microedition.lcdui.Displayable


      |

_  

javax.microedition.lcdui.Canvas


 

二,相关函数:

1 ,判断类

使用 hasPointerEvents 检测设备是否支持触摸,如果支持,则返回true

public boolean hasPointerEvents ()

Checks if the platform supports pointer press and release events.

Returns:

true if the device supports pointer events

 

使用 hasPointerMotionEvents 检测是否支持触摸动作,就是在触摸屏上拖拽,如果支持,则返回true

public boolean hasPointerMotionEvents ()

Checks if the platform supports pointer motion events (pointer dragged). Applications may use this method to determine if the platform is capable of supporting motion events.

Returns:

true if the device supports pointer motion events

 

2 ,取得坐标类(手指或者触摸笔之类触摸屏幕上的位置坐标)

protected void pointerPressed (int x,int y) // 触摸点的坐标

protected void pointerReleased (int x,int y) // 释放触摸点的坐标

protected void pointerDragged (int x,int y) // 触摸屏幕上拖拽的坐标,就是手指一直按在触摸屏上移动位置,该坐标会不停被刷新

 

三,如何使用

让我们的UI 类直接继承Canvas 类就可以了, 然后实现这几个函数,取得触摸的坐标

让程序去处理,比如:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class CMyApp extends Canvas{

    //position for touch

public int point_x = 0;

public int point_y = 0;

 

private void mainLoop(){

    if(point_x<50 && point_y<50)  {

        System.out.println("You point on the left top of screen!");

}

}

 

 

protected void pointerPressed( int x, int y){

    point_x = x;

    point_y = y;

}

 

protected void pointerReleased( int x, int y){

    point_x = x;

    point_y = y;

}   

protected void pointerDragged( int x, int y){

    point_x = x;

    point_y = y;

}

 

}

 

 

转载请保留以下信息:
作者(Author):smilelance
时间( Time ):2009.02
出处( From ):http://blog.csdn.net/smilelance

原文地址:https://www.cnblogs.com/secbook/p/2655445.html