-2

In Blackberry I want to create a class that extends HorizontalFieldManager so that I can display a label and an image on the same line with the label to the left and the image to the right.
I want the user to be able to interact with the HorizontalFieldManager as if it were a single field. I also want each HorizontalFieldManager to be focusable when added to a VeriticalFieldManager. I also want the clcik action events.

4

1 に答える 1

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);
    }
}
于 2011-01-14T12:21:54.170 に答える