如何在触摸编辑区域时自动隐藏虚拟键盘?

时间:2012-07-30 09:20:18

标签: java blackberry virtual-keyboard

在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);
    }
}
}

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

1 个答案:

答案 0 :(得分: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();
 }

至于其中来触发此代码,这是我推荐的内容,而不是覆盖onUnfocus()。我不确定这是解决问题的最简单,最有效的方法(所以我欢迎其他答案!),但我认为这样可行。

我之前告诉过你几个答案,你通常应该覆盖代码中的touchEvent()方法。对于像普通按钮这样的东西,我认为这是真的。这可能是您需要的一个示例。您应该有Manager(或VerticalFielManager或类似的)代表此EditField所在的屏幕。在该经理中,实施touchEvent()方法,如下所示:

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);
   }

试试吧。您可能需要更改我的逻辑,具体取决于您是否要隐藏键盘以进行完整的点击,而不是简单的触摸向下(如果您是BlackBerry新手,可能还不清楚这些之间有什么区别)。但是,我认为这应该让你接近(r)。