每周一技:HyperLinkField
Taigoo 发表于 July 14, 2009 7:35 pm
版权信息 :严禁转载, 若想推荐或收藏,请用链接的形式.
网址:http://www.inblackberry.com/web/development/week-tech/custom-field-hyperlink-field.html
这是每周一技的第二篇了。
前一篇介绍了从BES读取contacts的技术以及遇到相关问题的解决方案(via 每周一技:从BES服务器上lookup联系人)。
本篇将介绍如何编写自定义的超级链接控件。
背景
在很多情况下,我们都会遇见超级连接的显示,遗憾的是在Blackberry没有提供类似于HyperLinkField的组件,这给我们增加了很多麻烦,如果能够解决该问题,将会是一个不错的选择。
分析
既然Blackberry API没有,我们自己可以写一个该Field,在这里,我们将之命名为HyperLinkField,以符合Blackberry API命名的规则。
实现
先看几张效果图:
图1,当focus在其他field时HyperLinkField的样式.

图2 当光标移到该field上时,颜色会变蓝,当然你也可以更改颜色。

图3,当点击该Field时,

图4,其菜单如下

该Get Page就会打开连接。
在编写代码需要注意一下几个地方:
- 参数的设置
- Layout的处理
- 如何paint
- 菜单的处理
- 点击该field如何处理
下面给出部分重要的代码。
package com.taigoo.blackberry.weekskills.hyperlink;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.system.Display;
import net.rim.blackberry.api.browser.*;
public class HyperLinkField extends Field {
private String _displayName ;
private String _url;
private Font _font;
private MenuItem _openMenuItem;
/**
* Create a field for the hyperlink to implement the behaviour.
* @param text The text to display
* @param url The url to navigation
*/
public HyperLinkField(String text, String url) {
super();
this._displayName = text;
this._url = url;
this._font = Font.getDefault().
derive(Font.DOTTED_UNDERLINED).
derive(Font.UNDERLINED);
//create the menu item
this._openMenuItem = new MenuItem("Get Page",100000,10) {
public void run() {
openUrl();
}
};
}
/**
* set the display name
* @param name
*/
public void setDisplayName(String name) {
this._displayName = name;
}
/**
* Get the url
* @return returns the url
*/
public String getURL() {
return this._url;
}
/**
* @see net.rim.device.api.ui.Field#paint(Graphics)
*/
protected void paint(Graphics g) {
//draw text
g.setColor(Color.BLUE);
g.setFont(_font);
g.drawText(this._displayName, 0, 0,(int)(
getStyle() & DrawStyle.ELLIPSIS |
DrawStyle.HALIGN_MASK ),
this.getPreferredWidth() );
this.invalidate();
}
/**
* @see net.rim.device.api.ui.Field#layout(int, int)
*/
protected void layout(int width , int height) {
width = Math.min(this.getPreferredWidth(), width) ;
height = getPreferredHeight() ;
// set dimensions
setExtent( width, height );
}
protected void drawFocus(Graphics graphics,boolean on){
XYRect rect = new XYRect();
getFocusRect(rect);
graphics.setColor(Color.RED);
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
drawHighlightRegion(graphics, Field.HIGHLIGHT_FOCUS, on,
rect.x, rect.y, rect.width, rect.height);
this.invalidate();
}
protected boolean navigationClick(int status, int time){
openUrl();
return true;
}
private void openUrl() {
BrowserSession visitSession = Browser.getDefaultSession();
visitSession.displayPage(_url);
}
protected void makeContextMenu(ContextMenu contextMenu){
contextMenu.addItem(_openMenuItem);
contextMenu.setDefaultItem(_openMenuItem);
}
}
上面的代码已经很能说明问题了,我就不多解释了。
Field写好之后就是调用。调用也非常简单,和普通的代码一样,代码如下
HyperLinkField _hyperlinkField = new HyperLinkField(
"Click here to visit Inbalckberry",
"http://www.inblackberry.com/blog/wap");
this.add(_hyperlinkField);
每周一技术系列
