User interface 如何在Blackberry地图上的模型弹出窗口中显示自定义图标和两个超链接?

User interface 如何在Blackberry地图上的模型弹出窗口中显示自定义图标和两个超链接?,user-interface,blackberry,map,custom-controls,gps,User Interface,Blackberry,Map,Custom Controls,Gps,我需要在Blackberry地图上弹出的模型中显示一个自定义图标和两个超链接。我该怎么做? 首先实施按钮字段的扩展,该扩展将: 看起来像一个图标 单击将打开带有预定义链接的浏览器 将使用上下文菜单 此类控制的代码: class MapLinkIcon extends ButtonField implements FieldChangeListener { Bitmap mNormal; Bitmap mFocused; String mLink; String mDescripti

我需要在Blackberry地图上弹出的模型中显示一个自定义图标和两个超链接。我该怎么做?

首先实施按钮字段的扩展,该扩展将:

  • 看起来像一个图标
  • 单击将打开带有预定义链接的浏览器
  • 将使用上下文菜单
此类控制的代码:

class MapLinkIcon extends ButtonField implements FieldChangeListener {
 Bitmap mNormal;
 Bitmap mFocused;

 String mLink;
 String mDescription;

 int mWidth;
 int mHeight;

 public MapLinkIcon(Bitmap normal, Bitmap focused, String link,
   String description) {
  super(CONSUME_CLICK);
  mNormal = normal;
  mFocused = focused;
  mLink = link;
  mDescription = description;

  mWidth = mNormal.getWidth();
  mHeight = mNormal.getHeight();
  setMargin(0, 0, 0, 0);
  setPadding(0, 0, 0, 0);
  setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
  setBorder(VISUAL_STATE_ACTIVE, BorderFactory
    .createSimpleBorder(new XYEdges(0, 0, 0, 0)));

  this.setChangeListener(this);
 }

 protected void paint(Graphics graphics) {
  Bitmap bitmap = null;
  switch (getVisualState()) {
  case VISUAL_STATE_NORMAL:
   bitmap = mNormal;
   break;
  case VISUAL_STATE_FOCUS:
   bitmap = mFocused;
   break;
  case VISUAL_STATE_ACTIVE:
   bitmap = mFocused;
   break;
  default:
   bitmap = mNormal;
  }
  graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
    bitmap, 0, 0);
 }

 public int getPreferredWidth() {
  return mWidth;
 }

 public int getPreferredHeight() {
  return mHeight;
 }

 protected void layout(int width, int height) {
  setExtent(mWidth, mHeight);
 }

 protected void applyTheme(Graphics arg0, boolean arg1) {

 }

 public void fieldChanged(Field field, int context) {
  openLink(mLink);
 }

 MenuItem mMenuItem = new MenuItem("Go To Link", 0, 0) {
  public void run() {
   openLink(mLink);
  }
 };

 protected void makeContextMenu(ContextMenu contextMenu) {
  super.makeContextMenu(contextMenu);
  contextMenu.addItem(mMenuItem);
 }

 private static void openLink(String link) {
  Browser.getDefaultSession().displayPage(link);
 }
}
现在,我们可以将此按钮控件与MapField结合使用,覆盖子布局以将按钮放置在地图上:

class CustomMapField extends VerticalFieldManager {

 MapField mMapField;
 MapLinkIcon mButton;

 public CustomMapField() {
  add(mMapField = new MapField());
 }

 public int getPreferredHeight() {
  return getScreen().getHeight();
 }

 public int getPreferredWidth() {
  return getScreen().getWidth();
 }

 public void moveTo(Coordinates coordinates, Bitmap icoNorm, Bitmap icoAct,
   String link, String description) {

  mMapField.moveTo(coordinates);

  add(mButton = new MapLinkIcon(icoNorm, icoAct, link, description));
 }

 protected void sublayout(int maxWidth, int maxHeight) {
  int width = getPreferredWidth();
  int height = getPreferredHeight();
  layoutChild(mMapField, width, height);
  setPositionChild(mMapField, 0, 0);

  layoutChild(mButton, mButton.mWidth, mButton.mHeight);
  XYPoint fieldOut = new XYPoint();
  mMapField.convertWorldToField(mMapField.getCoordinates(), fieldOut);
  int xPos = fieldOut.x - mButton.mWidth / 2;
  int yPos = fieldOut.y - mButton.mHeight;
  setPositionChild(mButton, xPos, yPos);

  setExtent(width, height);
 }
}
使用示例:

class Scr extends MainScreen {
 CustomMapField mMapField;
 Coordinates mCoordinates;

 public Scr() {
  double latitude = 51.507778;
  double longitude = -0.128056;
  mCoordinates = new Coordinates(latitude, longitude, 0);

  mMapField = new CustomMapField();
  Bitmap icoNormal = Bitmap.getBitmapResource("so_icon_normal.png");
  Bitmap icoActive = Bitmap.getBitmapResource("so_icon_active.png");
  String link = "http://stackoverflow.com";
  String description = "StackOverflow";
  mMapField.moveTo(mCoordinates, icoNormal, icoActive, link, description);
  add(mMapField);
 }
}
另请参见: