Blackberry - Custom EditField Cursor

半世苍凉 提交于 2019-12-11 00:35:49

问题


I am creating a search box for my BlackBerry project, but it looks like BlackBerry doesn't have an API for creating a single line EditField. I have created a custom field by extending BasicEditField and overriding methods like layout and paint. In the paint function body, I am drawing a rectangle with getPreferredWidth() and getPreferredHeight(). But the cursor shows up at the default top-left position in the EditField. Can anybody tell me how I can draw the cursor where my text is? i.e. in the middle of EditField by calling drawText().


回答1:


I am not getting your question but.

  1. you can create single line edit field.

    BasicEditField editField = new BasicEditField(BasicEditField.NO_NEWLINE);

  2. you can set cursor position.

    editField.setCursorPosition(offset);




回答2:


You can draw the cursor where you want it to rewriting the onFocus method of the editField.

        protected void onFocus(int direction) {
            super.onFocus(direction);
            this.setCursorPosition(this.getTextLength());
            invalidate();
        }

After getting the Focus, the field will position the cursor at the end of the text. The editField has setCursorPosition and also getTextLength.

Hope it helps.




回答3:


Thank you guys, i got one fine solution from Blackberry Journal.

public class ScrollingSearchBox extends HorizontalFieldManager
{
    private int managerWidth;
    private int managerHeight;
    public ScrollingSearchBox()
    {
        super(Manager.NO_HORIZONTAL_SCROLL);
        searchEdit = new BasicEditField(){
            public int getPreferredHeight()
            {                 
                return ret;
            }
            public int getPreferredWidth()
            {                    
                return ret;
            }
            public void paint(Graphics g)
            {
                getManager().invalidate();
                super.paint(g);                    
            }
        };

        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()
    {
        int ret = 0;            
        return ret;
    }
    protected void sublayout(int maxWidth, int maxHeight)
    {
        int currX = 0;
        int currY = 0;
        Field currField;

        currField = this.getField(0);
        switch (ScreenConfig.getInstance().getScreen())
        {
            case ScreenConfig.SCREEN_320_240:
            currX = 5;
            currY = 3;
            break;
            case ScreenConfig.SCREEN_480_360:
            case ScreenConfig.SCREEN_480_320:
            currX = 5;
            currY = 1;
            break;
        }
        this.setPositionChild(currField, currX, currY);
        this.layoutChild(currField, currField.getPreferredWidth(),
        currField.getPreferredHeight());
        setExtent(this.getPreferredWidth(), this.getPreferredHeight());
    }

    protected void paint(Graphics graphics)
    {
        super.paint(graphics);
        graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
    }
} 

I am giving this so it can help others. Keep sharing your codes. Thanks.




回答4:


if you want to remove cursor you can override drawFocus of Field.

protected void drawFocus(Graphics graphics, boolean on) {} // remove cursor


来源:https://stackoverflow.com/questions/2662172/blackberry-custom-editfield-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!