不读取Blackberry中customEditField中输入的所有单词

时间:2014-04-08 15:13:21

标签: blackberry multiline blackberry-editfield

我正在使用Rim示例中给出的自定义EditField。 使用这个编辑域,我可以在一行中输入最大字符..但问题是,当我从edittext获取时,剩下的大多数隐藏字符都没有。

以下是我的CustomEditField类

  import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;

public class CustomEditfield extends Manager
{

    private final static int DEFAULT_LEFT_MARGIN = 0;
    private final static int DEFAULT_RIGHT_MARGIN = 5;
    private final static int DEFAULT_TOP_MARGIN = 0;
    private final static int DEFAULT_BOTTOM_MARGIN = 0;

    private final static int DEFAULT_LEFT_PADDING = 5;
    private final static int DEFAULT_RIGHT_PADDING = 5;
    private final static int DEFAULT_TOP_PADDING = 0;
    private final static int DEFAULT_BOTTOM_PADDING = 0;

    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;

    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;

    private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
    private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;

    private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;

    private int width ;
    private int height ;

    private EditField editField;
    private Bitmap _currentPicture; 

    public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
    {
        super(0);
        this._currentPicture=image;

        this.width=_currentPicture.getWidth();
        this.height=_currentPicture.getHeight()+20;
        editField = new EditField(label, initialValue, maxChars,style);
//        editField.setPadding(_currentPicture.getHeight()/7, 0, 0, _currentPicture.getWidth()/40);
        add(editField);
    }    


    protected void sublayout(int width, int height)
    {
        Field field = getField(0);
        layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
        setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
        setExtent(this.width, this.height);
    }

    public void setTopMargin(int topMargin)
    {
        this.topMargin = topMargin;
    }

    public void setBottomMargin(int bottomMargin)
    {
        this.bottomMargin = bottomMargin;
    }

    protected void paint(Graphics graphics)
    {
//        graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5);
        graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
        EditField ef = (EditField)getField(0);
        String entireText = ef.getText();

        String textToDraw = "";
        Font font = getFont();
        int availableWidth = width - totalHorizontalEmptySpace;
        if (font.getAdvance(entireText) <= availableWidth)
        {
            textToDraw = entireText;
        }
        else
        {
            int endIndex = entireText.length();
            for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
            {
                textToDraw = entireText.substring(beginIndex);
                if (font.getAdvance(textToDraw) <= availableWidth)
                {
                    break;
                }
            }
        }   

        ef.setText(textToDraw);

        super.paint(graphics);        

    }


    public int getPreferredWidth()
    {
        return width;
    }

    public int getPreferredHeight()
    {
        return height;
    }

    protected boolean keyChar(char ch, int status, int time)
    {
        if (ch == Characters.ENTER)
        {
            return true;
        }
        else
        {
            return super.keyChar(ch, status, time);
        }
    }

    public String getText()
    {
        return ((EditField)getField(0)).getText();
    }

    public void setText(final String text)
    {
        ((EditField)getField(0)).setText(text);
    }    
}

------在我的屏幕类---

et_fname = new CustomEditfield("", "", 50, EditField.NO_NEWLINE
            | EditField.FOCUSABLE, txt_bg);

所以当我打电话时

et_fname.getText().toString().trim()

它只会返回可见字符。 那么如何才能获得所有输入的字符。

如果有任何想法,请帮助.. 将不胜感激。

3 个答案:

答案 0 :(得分:2)

看看你的绘画方法。在那里你截断EditField,使它只包含适合的文本。那么你期望发生什么?

你可能认为你可以保存全文,然后调用super.paint()并恢复全文,但实际上这会引起你的其他问题。 EditField上的setText()将告诉字段它已被更新,因此它将尝试重新绘制自身。因此你将截断,它将标记为需要绘制的EditField,调用super.paint,replace,这将再次标记所需的绘制,然后EditField的绘制将启动并将尝试绘制我自己怀疑在调用管理器的paint()的过程中会出现递归和混乱。

这种只绘制可见的方法在管理器中是有缺陷的,永远不会起作用。

你似乎要做的是一行向左和向右滚动的EditField,更常见的方法是使用一个全宽的Horizo​​ntalFieldManager将EdtiField添加到那个并让HFM向左滚动EditField并且对。我没有找到这样做的示例代码,但我怀疑如果你看一下你会发现一些。那么请选择这个选项,让我们知道你是如何进行的。

答案 1 :(得分:2)

根据Peter Strange的建议,

我找到了解决我实际问题的单行编辑域的新解决方案。

以下是我的CustomEditField类,用于具有水平滚动的单行编辑字段。

 import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class CustomEditfield extends HorizontalFieldManager
{
    private int managerWidth;
    private int managerHeight;
    BasicEditField searchEdit;
    private Bitmap _currentPicture;
    private int width ;
    private int height ;
    private final static int DEFAULT_LEFT_MARGIN = 0;
    private final static int DEFAULT_RIGHT_MARGIN = 5;
    private final static int DEFAULT_TOP_MARGIN = 0;
    private final static int DEFAULT_BOTTOM_MARGIN = 0;

    private final static int DEFAULT_LEFT_PADDING = 5;
    private final static int DEFAULT_RIGHT_PADDING = 5;
    private final static int DEFAULT_TOP_PADDING = 0;
    private final static int DEFAULT_BOTTOM_PADDING = 0;

    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;

    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;

    private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
    private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;

    public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
    {
        super(Manager.NO_HORIZONTAL_SCROLL);
        this._currentPicture=image;
        this.width=_currentPicture.getWidth();
        this.height=_currentPicture.getHeight()+20;

        searchEdit = new BasicEditField(label, initialValue, maxChars,style){
            public int getPreferredHeight()
            {                 
                return _currentPicture.getHeight();
            }
            public int getPreferredWidth()
            {                    
                return _currentPicture.getWidth();
            }
//            public void paint(Graphics g)
//            {
//                getManager().invalidate();
//                super.paint(g);                    
//            }
        };
        searchEdit.setPadding(_currentPicture.getHeight()/7, 0, 0, 0);

        HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
        {
            public void sublayout(int width, int height)
            {
                if (managerWidth == 0) {
                    managerWidth = searchEdit.getPreferredWidth();
                }
                if (managerHeight == 0) {
                    managerHeight = searchEdit.getPreferredHeight();
                }
                super.sublayout(managerWidth, managerHeight);
                setExtent(managerWidth,managerHeight);
            }
            public void paint(Graphics g) {
                super.paint(g);
            }
        };
//        searchEdit.setMaxSize(70);
        hfm.add(searchEdit);
        add(hfm);
    }

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

    public String getText()
    {
        return searchEdit.getText();
    }

    public void setText(final String text)
    {
        searchEdit.setText(text);
    } 

    protected void sublayout(int maxWidth, int maxHeight)
    {
        Field currField;

        currField = this.getField(0);
        this.setPositionChild(currField,leftMargin+leftPadding, topMargin+topPadding);
        this.layoutChild(currField, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
        setExtent(this.width, this.height);
    }

    protected void paint(Graphics graphics)
    {
        graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
//      graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
        super.paint(graphics);
//        graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
    }
}

我从Blackberry - Custom EditField Cursor

找到了这个解决方案

我这样做可以帮助别人。

答案 2 :(得分:0)

问题是当你用字符占用字段宽度时输入字符。它将采用新角色并在场中删除旧角色。因此,当您在字段中获取数据时,您只能在字段中获得字符。

您可以按照以下步骤重现这一点: 1.输入至少8个字符。 2.回到关键。 3.然后来到第一个角色,这不是你输入的角色。