views:

429

answers:

3

hey in my screen there is a an edit field and 2 custom button fields as "OK" and "CANCEL" Below buttonfield there are some more focussable label fields

when i write a name in edit field and press enter then focus comes to "OK" button but how to set focus on "CANCEL" button.

Moreover while scrolling the focus does not automatically move ahead???

what to do may be i m confused with touch events and their handling!!!

Kindly help!!!!!!!!!!!!

Code:

        txt_Name = new EditField(TextField.NO_NEWLINE)
            {
                public void paint(net.rim.device.api.ui.Graphics g)
                {
                    g.setColor(Color.MAROON);
                    super.paint(g);
                }
           };

            txt_Name.setFont(font);

     v1 =  new VerticalFieldManager();
     v1.add(txt_Name );


    ButtonField btn1 = new ButtonField("OK",ButtonField.CONSUME_CLICK);
    ButtonField btn2 = new ButtonField("CANCEL",ButtonField.CONSUME_CLICK);

     v2 =  new VerticalFieldManager();
     v2.add(btn1);
     v2.add(btn2);

    LabelField l1 = new  LabelField("Hello Moon ",Field.Focussable);
    LabelField l2 = new  LabelField("Hello Citizen",Field.Focussable);
    LabelField l3 = new  LabelField("Hello People",Field.Focussable);
    LabelField l4 = new  LabelField("Hello world",Field.Focussable);

     v3 =  new VerticalFieldManager();
     v3.add(l1);
     v3.add(l2);
     v3.add(l3);
     v3.add(l4);

    add(v1);
    add(v2);
    add(v3);
}


protected boolean navigationClick(int status, int time) 
    {
     if(OK.isFocus())
             {
                //execute some code
                return true;
             }
if(CANCEL.isFocus())
             {
                //execute some code
                return true;
             }
        }
A: 

You can control the focus order by overriding the "navigationMovement" method on the screen or manager containing the focusable fields, such as the okay and cancel buttons. Just evaluate the arguments passed in to that method, set focus on the desired field using Field.setFocus(), and return true.

As for the focus not moving ahead with scrolling - that's just the way the touchscreen UI works. The focus only moves when you touch a focusable field - so if you aren't touching any focusable fields when you "flick" the screen to scroll, the focus won't change.

Marc Novakowski
well marc the problem that i face is like ::when i enter a name in the edit field say "hello",after this i touch the ok buttonbut since the focus is still on the edit field so when i touch the ok button then its corresponding code does not execute..i tried working with TouchEvent,navigationClick,navigationmovementbut cldnt find a solution,my button does not invoke,what to do???
SWATI
Trying setting a FieldChangeListener on each button instead of using the screen's navigationClick.
Marc Novakowski
Marc i tried what u said with buttonField but still doesnt workinfact nw i have used a custom Field in place of button which is a focussable Field havig image but it also does not work on that..
SWATI
dont knw what i m doing wrong!!!!!!!
SWATI
+1  A: 

Hi Swati!

I made just like Mark suggested, separate FieldChangeListeners for each button:

class Scr extends MainScreen {
    EditField txt_Name;
    ButtonField btnOK;
    ButtonField btnCancel;
    VerticalFieldManager v1;
    VerticalFieldManager v2;
    VerticalFieldManager v3;
    Font font = Font.getDefault().derive(Font.BOLD, 20);

    public Scr() {
        txt_Name = new EditField(TextField.NO_NEWLINE) {
            public void paint(net.rim.device.api.ui.Graphics g) {
                g.setColor(Color.MAROON);
                super.paint(g);
            }
        };

        txt_Name.setFont(font);

        v1 = new VerticalFieldManager();
        v1.add(txt_Name);

        btnOK = new ButtonField("OK", ButtonField.CONSUME_CLICK);
        btnOK.setChangeListener(
            new FieldChangeListener(){
                public void fieldChanged(Field field, int context) {
            Dialog.inform("OK pressed");
        }});
        btnCancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
        btnCancel.setChangeListener(
            new FieldChangeListener(){
                public void fieldChanged(Field field, int context) {
            Dialog.inform("Cancel pressed");
        }});
        v2 = new VerticalFieldManager();
        v2.add(btnOK);
        v2.add(btnCancel);

        LabelField l1 = new LabelField("Hello Moon", Field.FOCUSABLE);
        LabelField l2 = new LabelField("Hello Citizen", Field.FOCUSABLE);
        LabelField l3 = new LabelField("Hello People", Field.FOCUSABLE);
        LabelField l4 = new LabelField("Hello world", Field.FOCUSABLE);

        v3 = new VerticalFieldManager();
        v3.add(l1);
        v3.add(l2);
        v3.add(l3);
        v3.add(l4);

        add(v1);
        add(v2);
        add(v3);
    }
}

Now it seems to be OK:
alt text alt text alt text

UPDATE

Swati I can click those buttons with Storm simulator using a mouse click. I can't find any other explanation than cod file in simulator is from old version. You can check it quickly by changing any label text in code and then deploy and check if this change will be applied in app on device. In case if not, this is the old version and you should clean simulator and deploy app once again.
Hope this will help you!
See also BlackBerry - Changes are not getting reflected in my app

Max Gontar
max i am working in the same way but still focus does not comes on the ok n cancel button whe i press itits so simple still unable to doGosh what am i doing wrong??????????well thank u so much fr the effort....let me find what stupid mistake i m doing
SWATI
hey focus comes on the buttons when i press arrow keys from keyboardnow what to do..
SWATI
i deleted my code nad worked with yours it worked!!!!i think in my case the problem is with custom button field on which focus does not appear.....
SWATI
If help needed with that custom button, post new question with code, so we can try it ;)
Max Gontar
hey max the problem arises when i scroll up the screen.....now what to do
SWATI
Swati, please describe, what problem arises, how you scroll the screen (with mouse or PC keyboard in case of simulator), the reason why scrolling became available (more controls, lot of entered text, orientation change etc)
Max Gontar
Hey Max my problem is resolvedi placed the buttons inside a horizontal field manager.On navigation click i fetched their focus index and my buttons worked..God knows y it did not worked without placing buttons in horizontal field manager
SWATI
A: 
    txt_Name = new EditField(TextField.NO_NEWLINE)
            {
                public void paint(net.rim.device.api.ui.Graphics g)
                {
                    g.setColor(Color.MAROON);
                    super.paint(g);
                }
           };

            txt_Name.setFont(font);

     v1 =  new VerticalFieldManager();
     v1.add(txt_Name );


    ButtonField btn1 = new ButtonField("OK",ButtonField.CONSUME_CLICK);
    ButtonField btn2 = new ButtonField("CANCEL",ButtonField.CONSUME_CLICK);

     h2 =  new HorizontalalFieldManager();
     h2.add(btn1);
     h2.add(btn2);

    LabelField l1 = new  LabelField("Hello Moon ",Field.Focussable);
    LabelField l2 = new  LabelField("Hello Citizen",Field.Focussable);
    LabelField l3 = new  LabelField("Hello People",Field.Focussable);
    LabelField l4 = new  LabelField("Hello world",Field.Focussable);

     v3 =  new VerticalFieldManager();
     v3.add(l1);
     v3.add(l2);
     v3.add(l3);
     v3.add(l4);

    add(v1);
    add(h2);
    add(v3);
}


protected boolean navigationClick(int status, int time) 
    {
int index = h2.getFieldwithFocusIndex();
     if(h2==0)
             {
                //execute some code for OK
                return true;
             }
if(h2==1)
             {
                //execute some code for cancel
                return true;
             }
        }
SWATI