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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
jssantosh84
Posts: 94
Registered: ‎05-29-2010
My Carrier: asfas
Accepted Solution

clear the editfields on pressing the escape(back on the keyboard)

hi all,

I want to clear all the editFields on pressing the escape key. I know that i have to implement a keyListener but what i don't know what to add to clear the editFields.

 

If anybody knows how to do this please share.

 

Thanks

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Carrier: various

Re: clear the editfields on pressing the escape(back on the keyboard)

 

Field curField = UiApplication.getUiApplication.getActiveScreen().getLeafFieldWithFocus();
if ((curField != null) && (curField instanceof EditField)) {
  ((EditField)curField).setText("");
  curField.invalidate();
}

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
jssantosh84
Posts: 94
Registered: ‎05-29-2010
My Carrier: asfas

Re: clear the editfields on pressing the escape(back on the keyboard)

I want to clear all the editFields of the previous screen on pressing the escape key. i think this might clear only the fields with focus(of the current screen).

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Carrier: various

Re: clear the editfields on pressing the escape(back on the keyboard)

Just go through each field and if it is EditField setText to "".

 

getScreenBelow(), getFieldCount(), getField() and instanceof are your friends.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
jssantosh84
Posts: 94
Registered: ‎05-29-2010
My Carrier: asfas

Re: clear the editfields on pressing the escape(back on the keyboard)

since i m implementing this in a keylistener class, which will be adde to almost all screens, so this doesn't seem to work out

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Carrier: various

Re: clear the editfields on pressing the escape(back on the keyboard)

Then add UiApplication.getUiApplication().getActiveScreen() to the picture. I guess you want to pop that screen and "clean" the previous screen (which is about to show up) before doing so.

 

Something like that:

 

Screen activeScr = UiApplication.getUiApplication().getActiveScreen();
Screen nextScreen = null;
if (activeScr != null) {
  nextScreen = activeScr.getScreenBelow();
}
if (nextScreen != null) {
  int count = nextScreen.getFieldCount();
  for (int i = count - 1; i >= 0; --i) {
    Field curField = nextScreen.getField(i);
    if (curField instanceof TextField) {
      ((TextField) curField).setText("");
    }
  }
}

 

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
jssantosh84
Posts: 94
Registered: ‎05-29-2010
My Carrier: asfas

Re: clear the editfields on pressing the escape(back on the keyboard)

i solved the problem. i am poping the below screen, creating a new instance of that particular screen and pushing it using invokeLater

 

public class KeyListenerImplementation implements KeyListener
{

     private Screen belowScreen;

    public boolean keyChar( char key, int status, int time )
    {

         if ( key == Characters.ESCAPE && UiApplication.getUiApplication().getScreenCount()>2)
        {
            belowScreen = UiApplication.getUiApplication().getActiveScreen().getScreenBelow();
            
            if(belowScreen instanceof  XXScreen){
                
                UiApplication.getUiApplication().invokeLater(new Runnable(){
                    public void run() {
                        UiApplication.getUiApplication().popScreen(belowScreen);

                        UiApplication.getUiApplication().pushScreen(new XXScreen));
                    }
                    });
            }

}}

Please use plain text.