BasicEditField自定义

时间:2010-01-05 17:48:02

标签: blackberry user-interface

我想要一个基本的编辑字段,其中输入的第一个字符位于最右边的位置......类似这样......

                  2

                 23

                234

任何人都可以告诉我如何做到这一点......

4 个答案:

答案 0 :(得分:2)

这是一种方法:

import net.rim.device.api.system.Characters;
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.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.BasicEditField;

public class RightInsertTextField extends BasicEditField {
    private String lastGoodTxtInputEntry = null;
    private int maxCharInputLength = 20;
    private Field nextFieldForFocus = null;

/**
 * Basic constructor.
 * 
 * @param defaultValue
 */
public RightInsertTextField(String defaultValue) {
    super("", defaultValue);
}

public void paint(Graphics g) {
    String txt = this.getText();
    // 1. (Optional) keeping a check on input length can help
    // minimize custom code needed to handle wrap-around text.
    if (txt.length() > this.getMaxCharInputLength() && this.getMaxCharInputLength() > 0) {
        txt = this.lastGoodTxtInputEntry;
        this.setText(txt);
    } else {
        this.lastGoodTxtInputEntry = txt;
    }

    // 2. get rid of the default cursor painting by coloring over it with
    // the same color as the background
    XYRect xy = g.getClippingRect();
    g.setBackgroundColor(Color.WHITE);
    g.fillRect(xy.x, xy.y, xy.width, xy.height);
    g.clear();

    // 3. Align text to the right.

    g.setColor(Color.BLACK);
    g.drawText(txt, 0, 0, (DrawStyle.RIGHT + DrawStyle.ELLIPSIS), getWidth());

}

/**
 * Look out for 'ENTER' being pressed.
 */
public boolean keyChar(char key, int status, int time) {
    // Prevent new lines in input field.
    if (Characters.ENTER == key) {
        if (this.nextFieldForFocus != null) {
            this.nextFieldForFocus.setFocus();
        }
        return true;
    } else {
        return super.keyChar(key, status, time);
    }
}

public void setMaxCharInputLength(int maxCharInputLength) {
    this.maxCharInputLength = maxCharInputLength;
}

public int getMaxCharInputLength() {
    return maxCharInputLength;
}

/**
 * @param nextFieldForFocus
 *            the nextFieldForFocus to set if 'ENTER' is pressed.
 */
public void setNextFieldForFocus(Field nextFieldForFocus) {
    this.nextFieldForFocus = nextFieldForFocus;
}

}

答案 1 :(得分:2)

这会有所帮助

editField = new EditField("", "", maxChars, EditField.NO_NEWLINE | EditField.NON_SPELLCHECKABLE){
    private String text = "";
    protected boolean keyChar(char key, int status, int time){
        switch (key){
            case Characters.BACKSPACE:{
                try {
                    text = text.substring(0,text.length()-1);
                    invalidate();
                } catch (IndexOutOfBoundsException e) {}
                return true;
            }
        }
        text = text + key;
        invalidate();
        return true;
    }
    protected void paint(Graphics graphics) {
        graphics.drawText(text,0, 0, DrawStyle.RIGHT, width - 10);
        super.paint(graphics);
    }
};

答案 2 :(得分:0)

使用样式Field.FIELD_RIGHT。

答案 3 :(得分:0)

非常确定没有内置的方法来执行此操作,您必须实现自己的文本框