BlackBerry自定义Horizo​​ntalFieldManager

时间:2011-01-07 07:57:42

标签: blackberry horizontalfieldmanager

在Blackberry中我想创建一个扩展Horizo​​ntalFieldManager的类,以便我可以在同一行显示标签和图像,左边是标签,右边是图像。
我希望用户能够与Horizo​​ntalFieldManager进行交互,就像它是单个字段一样。我还希望每个Horizo​​ntalFieldManager在添加到VeriticalFieldManager时都是可聚焦的。我也想要clcik行动事件。

1 个答案:

答案 0 :(得分:1)

听起来你会想要开始编写自己的Field类,这是一个让你入门的例子:


import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class SimpleField extends Field {

    private String label;
    private Bitmap image;
    private int fieldWidth;
    private int fieldHeight;
    private boolean hover = false;
    private int focusColor = 0xcccccc;

    public SimpleField(String label, Bitmap image) {
        super(Field.FOCUSABLE);
        this.label = label;
        this.image = image;
        fieldWidth = Display.getWidth();
        fieldHeight = image.getHeight();
    }

    protected void onFocus(int direction) {
        hover = true;
        invalidate();
        super.onFocus(direction);
    }

    protected void onUnfocus() {
        hover = false;
        invalidate();
        super.onUnfocus();
    }

    public int getPreferredWidth() {
        return fieldWidth;
    }

    public int getPreferredHeight() {
        return fieldHeight;
    }

    protected void layout(int width, int height) {
        setExtent(fieldWidth, fieldHeight);
    }

    protected void paint(Graphics graphics) {
        if(hover){
            graphics.setColor(focusColor);
            graphics.fillRect(0, 0, fieldWidth, fieldHeight);
        }
        graphics.drawText(label, 0, (fieldHeight - graphics.getFont().getHeight()) / 2);
        graphics.drawBitmap(graphics.getFont().getAdvance(label), 0, image.getWidth(), image.getHeight(), image, 0, 0);
    }
}