Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
salmankhan7788
Posts: 52
Registered: ‎02-26-2013
My Device: Curve 9650,Bold 9900
My Carrier: Airtel
Accepted Solution

How to set/move Focus automatically to next editfield ?

Hi

I am working on an app in which i need to four editfield as a key field, in these i set the setMaxSize 4,This works fine..

 

Now i want when user enter four letter in these editfield the focus automatically move to next field.

please provide any code reference or logic.

 

Thanks in advance.

Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10
My Carrier: Vodafone

Re: How to set/move Focus automatically to next editfield ?

calculate length of edit field if editfield length is 4 in focus change listener method remove focus on previous edit field and set it to new edit field u want..
Please use plain text.
Regular Contributor
salmankhan7788
Posts: 52
Registered: ‎02-26-2013
My Device: Curve 9650,Bold 9900
My Carrier: Airtel

Re: How to set/move Focus automatically to next editfield ?

//Here is my code where i need to use this logic...?

 

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.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class CustomEditField2 extends HorizontalFieldManager implements DrawStyle
    {
        private int _managerWidth    = 0;
        private int _managerHeight    = 0;
        private BasicEditField _editField    = null;
        HorizontalFieldManager _hfm = null;
        boolean _optionalFlag    = false;
        Graphics _graphics         = null;
        int _autoScrollPos        = 0;
        int _scrollAmount        = 0;
        HorizontalFieldManager _scollableManager = null;
        String _hint;
        long _fieldStyle = 0;
        FontFamily fm[]=FontFamily.getFontFamilies();
        Font font = fm[0].getFont(FontFamily.SCALABLE_FONT,6,Ui.UNITS_pt);
        /**
         * @param width width of the field
         * @param height height of the field
         * @param optional wether this is an optional field or not
         * @param scrollPos scrolling position index. 1 for 1st field. 2 for second field
         * @param scollableManager vertical field manager where the fields are laying.
         * @param fieldStyle preffered field style.
         */
        public CustomEditField2(int width, int height, String hint,boolean optional,int scrollPos, HorizontalFieldManager scollableManager ,long fieldStyle)
        {
            super(HorizontalFieldManager.NO_HORIZONTAL_SCROLL|fieldStyle);
            _managerWidth     = width;
            _managerHeight     = height;
            _hint            = hint;
            _optionalFlag   = optional;
            _autoScrollPos    = scrollPos;
            _scrollAmount    = _autoScrollPos * 10;
            _scollableManager = scollableManager;
            _fieldStyle        = fieldStyle;
            //_hfm = new HorizontalFieldManager(HorizontalFieldManager.HORIZONTAL_SCROLL);
            _hfm = new HorizontalFieldManager();
            
            
            _editField = new BasicEditField("", "", 4,FIELD_HCENTER | EditField.NO_NEWLINE | _fieldStyle|BasicEditField.FILTER_NUMERIC)
            {
                
                public void paint(Graphics graphics)
                {
                    if(getText().trim().length() > 0)
                    {
                        _graphics = graphics;
                        graphics.setColor(Color.BLACK);
                        if(isFocus())
                        {
                            graphics.drawText(getText(), 0, 0);
                        }
                        else
                        {
                            graphics.drawText(getText(), 0, 0,DrawStyle.ELLIPSIS,_managerWidth);
                        }
                    }
                    else if(_optionalFlag)
                    {
                        graphics.setColor(Color.GRAY);
                        
                        graphics.drawText(_hint, 0, 0);
                        
                        if(!isFocus() && getText().trim().length() > 0)
                        {
                            graphics.drawText(getText(), 0,  0,DrawStyle.ELLIPSIS,_managerWidth);
                        }
                    }
                }
                protected boolean keyDown(int keycode, int time)
                {
                    char keyChar = Keypad.map(keycode);
                    if(keyChar == Characters.ENTER && _scollableManager != null)
                    {
                        _scollableManager.setVerticalScroll(_scrollAmount);
                    }
                    return super.keyDown(keycode, time);
                }
                
                protected boolean keyUp(int keycode, int time)
                {
                    
                    /*if(StaticStore.isLogin)
                    {
                    if(!RegistrationScreen.oldText.equalsIgnoreCase(_editField.getText()))
                    {
                        RegistrationScreen.submitBtn.setLabel("Update");
                    }
                    else
                    {
                        RegistrationScreen.submitBtn.setLabel("Submit");
                    }
                    }*/
                    return super.keyUp(keycode, time);
                }
                
            };
            _hfm.add(_editField);
            _editField.setMargin((_managerHeight-_editField.getPreferredHeight())/2, 0, 0, 0);
            add(_hfm);
        }
        
        public void setEditable(boolean editable)
        {
            _editField.setEditable(editable);
        }
        
        public void setMaxChar(int maxChar)
        {
            _editField.setMaxSize(4);
        }
        
        public void setFocus()
        {
            _editField.setFocus();
        }
        
        /*public void focusChangeNotify(int arg0)
        {
            if(!_editField.isFocus() )
            {
                if(_editField.getText().trim().length() > 0)
                {
                    _hfm.deleteAll();
                    _editField.setText(_editField.getText().trim());
                    _hfm.add(_editField);
                }
                
            }
            else
            {
                if(_scollableManager != null)
                    _scollableManager.setVerticalScroll(_scrollAmount);
            }
            super.focusChangeNotify(arg0);
        }*/
        
        public void sublayout(int width, int height)
        {
            if (_managerWidth == 0)
                _managerWidth = 260;
            
            if (_managerHeight == 0)
                _managerHeight = 25;
            
            super.sublayout(_managerWidth, _managerHeight);
            setExtent(_managerWidth, _managerHeight);
        }
        
        protected void paint(Graphics graphics)
        {
            graphics.clear(0,0, _managerWidth, _managerHeight);
            super.paint(graphics);
        }
        protected void paintBackground(Graphics g)
        {
        // TODO Auto-generated method stub
            g.setBackgroundColor(Color.WHITE);
        super.paintBackground(g);
        }
        
        public String getTextString()
        {
            return _editField.getText();
        }

        public void setTextString(String text)
        {
            _editField.setText(text);
        }
        
        public void setFontSize(int fontSize)
        {
             FontFamily fm[]=FontFamily.getFontFamilies();
             font = fm[0].getFont(FontFamily.SCALABLE_FONT, fontSize, Ui.UNITS_pt);
            _editField.setFont(font);
        }
        
        public Font getFont(String text)
        {
            return _editField.getFont();
        }
        
        protected void onFocus(int direction)
        {
        //    RegistrationScreen.oldText=_editField.getText();
        super.onFocus(direction);
        }
    }

Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10
My Carrier: Vodafone

Re: How to set/move Focus automatically to next editfield ?

Use it in screen where u r calling this editbox class
Please use plain text.
Developer
simon_hain
Posts: 15,805
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: How to set/move Focus automatically to next editfield ?

i would start with adding the editfields to their own data structure (a vector, for example), makes it easier to access them.

you can overwrite keyChar on the editfield and check the text length, if it is the maximum length you call setFocus on the next editfield (if any).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Regular Contributor
salmankhan7788
Posts: 52
Registered: ‎02-26-2013
My Device: Curve 9650,Bold 9900
My Carrier: Airtel

Re: How to set/move Focus automatically to next editfield ?

[ Edited ]

/Here i used like this and its work fine..here key1,key2,key3 and key4 are editfields..now i face a new problem when i need to go back to any field and need to change any field value..it does not allow me to change and focus automatically goes to last field i mean key4, here what i need to do..?

 

  protected boolean keyChar(char c, int status, int time)
       {
        // TODO Auto-generated method stub
           int len1 = key1.getTextString().length();
           int len2 = key2.getTextString().length();
           int len3 = key3.getTextString().length();
           int len4 = key4.getTextString().length();
           
           
           if(key1.isFocus() && len1==4)
           {
             key2.setFocus();
           }
           else if(key2.isFocus() && len2==4)
           {
                   key3.setFocus();
           }
           else if(key3.isFocus() && len3==4)
           {
                   key4.setFocus();
           }
       else

        {

        } 
        return super.keyChar(c, status, time);
    }

Please use plain text.
Developer
developer_aman_arora
Posts: 354
Registered: ‎06-05-2013
My Device: Blackberry Z10
My Carrier: Vodafone

Re: How to set/move Focus automatically to next editfield ?

all the best
Please use plain text.