예제 #1
0
  protected boolean touchEvent(TouchEvent te) {
    System.out.println("touchEvent type " + Integer.toString(te.getEvent()));
    if (te.getEvent() == TouchEvent.CANCEL) return true;
    int x = te.getX(1);
    int y = te.getY(1);
    if (x < 0 || x > this.getWidth() || y < 0 || y > this.getHeight()) {
      Logger.log(
          "touchedEvent type "
              + Integer.toString(te.getEvent())
              + " x="
              + Integer.toString(x)
              + " y="
              + Integer.toString(x)
              + " outside "
              + Integer.toString(getWidth())
              + "x"
              + Integer.toString(getHeight()));
      return super.touchEvent(te);
    }
    switch (te.getEvent()) {
      case TouchEvent.DOWN:
        touchDownEvent(x, y);
        break;
      case TouchEvent.UP:
        touchUpEvent(x, y);
        break;
      case TouchEvent.MOVE:
        touchMovedEvent(x, y);
        break;
      default:
        return super.touchEvent(te);
    }

    return true;
  }
  protected boolean touchEvent(TouchEvent message) {
    boolean isConsumed = false;
    boolean isOutOfBounds = false;
    int x = message.getX(1);
    int y = message.getY(1);
    // Check to ensure point is within this field
    if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
      isOutOfBounds = true;
    }
    switch (message.getEvent()) {
      case TouchEvent.CLICK:
      case TouchEvent.MOVE:
        if (isOutOfBounds) return true; // consume
        _selected = true; // Pressed effect

        // update state
        int stateWidth = getExtent().width / _numStates;
        int numerator = x / stateWidth;
        int denominator = x % stateWidth;
        if (denominator > stateWidth / 2) {
          numerator++;
        }
        _currentState = numerator;
        invalidate();

        isConsumed = true;
        break;
      case TouchEvent.UNCLICK:
        if (isOutOfBounds) {
          _selected = false; // Reset presssed effect
          return true;
        }

        // A field change notification is only sent on UNCLICK to allow for recovery
        // should the user cancel, i.e. click and move off the button

        _selected = false; // Reset pressed effect

        // Update state
        stateWidth = getExtent().width / _numStates;
        numerator = x / stateWidth;
        denominator = x % stateWidth;
        if (denominator > stateWidth / 2) {
          numerator++;
        }
        _currentState = numerator;
        invalidate();

        fieldChangeNotify(0);

        isConsumed = true;
        break;
    }
    return isConsumed;
  }