1

在android中,我可以这样做,用户可以在editview之外单击以隐藏虚拟键盘。

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                        .getBottom())) {

            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                    .getWindowToken(), 0);
        }
    }
    return ret;
}

黑莓呢?我只想跑VirtualKeyboard.isSupported()

更新

public class Custom_EditField extends EditField {
private int width, row, color;
private MainScreen mainscreen;

Custom_EditField(long style, int width, int row, MainScreen mainscreen) {
    super(style);
    this.width = width;
    this.row = row;
    this.mainscreen = mainscreen;
}

public int getPreferredHeight() {
    return Font.getDefault().getHeight() * row;
}

public int getPreferredWidth() {
    return width;
}

protected void onFocus(int direction) {
    if (VirtualKeyboard.isSupported())
        mainscreen.getVirtualKeyboard().setVisibility(
                VirtualKeyboard.SHOW_FORCE);
    invalidate();
    super.onFocus(direction);
}

protected void onUnfocus() {
    if (VirtualKeyboard.isSupported())
        mainscreen.getVirtualKeyboard().setVisibility(
                VirtualKeyboard.HIDE_FORCE);
    invalidate();
    super.onUnfocus();
}

public boolean isFocusable() {
    return true;
}

protected void layout(int maxWidth, int maxHeight) {
    super.layout(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
    super.setExtent(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
}

protected void paint(Graphics graphics) {
    int rectHeight = getPreferredHeight();
    int rectWidth = getPreferredWidth();
    try {
        color = Color.BLACK;
        graphics.setColor(color);
        graphics.drawRect(0, 0, rectWidth, rectHeight);
        super.paint(graphics);
    } finally {
        graphics.setColor(color);
    }
}
}

如果您单击另一个字段而不是任何点,此编辑字段将隐藏键盘。

4

1 回答 1

1

我有用于显示或隐藏键盘的实用程序代码。这应该对 OS 4.7 及更高版本有效。如果您需要支持较低的操作系统版本,请告诉我。

   /** Hides the virtual keyboard, if there is one showing. */
   public static void hideKeyboard() {
      VirtualKeyboard kb = UiApplication.getUiApplication().getActiveScreen().getVirtualKeyboard();
      if (kb != null) {
         kb.setVisibility(VirtualKeyboard.HIDE);
      }
   }

   /** @return TRUE if the virtual keyboard is hidden, or not supported */
   public static boolean isKeyboardHidden() {
      if (VirtualKeyboard.isSupported()) {
         VirtualKeyboard kb = UiApplication.getUiApplication().getActiveScreen().getVirtualKeyboard();
         if (kb != null) {
            int visibility = kb.getVisibility();
            return ((visibility == VirtualKeyboard.HIDE)
                    || (visibility == VirtualKeyboard.HIDE_FORCE));
         }
      }
      return true;
   }

请注意,我制作了这些static功能。所以,如果你把它们放在一个名为 的类中UiUtilities,那么你会这样称呼它们:

 if (!UiUtilities.isKeyboardHidden()) {
     UiUtilities.hideKeyboard();
 }

As far as where to trigger this code, here's what I recommend, instead of overriding onUnfocus(). I'm not sure this is the easiest, or most efficient way to solve the problem (so I welcome other answers!), but I think this will work.

I told you a couple answers ago that you normally should not override the touchEvent() method in your code. For things like normal buttons, I think that's true. This might be one example where you need to. You should have a Manager (or VerticalFielManager, or similar) that represents the screen that this EditField is on. In that manager, implement the touchEvent() method like this:

import net.rim.device.api.ui.TouchEvent;

   protected boolean touchEvent(TouchEvent event) {
      // We take action when the user completes a click (a.k.a. unclick)
      int eventCode = event.getEvent();
      if ((eventCode == TouchEvent.UNCLICK) || (eventCode == TouchEvent.DOWN)) {
         // Get the touch location, within this Manager
         int x = event.getX(1);
         int y = event.getY(1);

         if ((x >= 0) && (y >= 0) && (x < getWidth()) && (y < getHeight())) {
            int field = getFieldAtLocation(x, y);
            if (field >= 0) {
               // Let event propagate to child field
               return super.touchEvent(event);
            } else {
               if (eventCode == TouchEvent.UNCLICK) {
                  // A completed click anywhere else in this manager should dismiss the keyboard
                  UiUtilities.hideKeyboard();
               } else {
                  // This is just a soft touch (TouchEvent.DOWN), without full click
                  setFocus();
               }
               // Consume the event
               return true;
            }
         }
      }
      // Event wasn't for us, let superclass handle in default manner
      return super.touchEvent(event);
   }

Try that. You might need to change my logic, depending on whether you want to hide the keyboard for a full click, versus a simple touch down (if you're new to BlackBerry, it might not be clear what the difference between those are). But, I think this should get you close(r).

于 2012-07-30T09:37:48.003 回答