问题描述:

In android, I can do like that where user can click outside of editview to hide the virtual keyboard.

@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;

}

What about in blackberry? I want to run for VirtualKeyboard.isSupported() only.

Update

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

}

}

}

This editfield will hide the keypad if you click on another field but not anypoint.

网友答案:

I have this utility code for showing, or hiding the keyboard. This should be valid for OS 4.7 and above. Let me know if you need to support lower OS versions.

   /** 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;
   }

Note that I made these static functions. So, if you put them in a class named UiUtilities, then you would call them like:

 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).

相关阅读:
Top