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
Developer
Posts: 88
Registered: ‎07-11-2009
My Device: Not Specified

EditField Problems within VerticalFieldManager related to keyChar

have three EditField objects within a VerticalFieldManager.

 

My goal is to have a Listener detect each key pressed by the user.

 

Particularly if the user presses ENTER then it will move on to the next field in the VerticalFieldManager.  I have been able to do this but the problem is when the user scrolls to the middle of the text and starts typing the new characters are placed at the end, or when they start deleting text.

 

I know where the problem lies but I actually don't know how to fix it.  Any help would be appreciated.

 

Here's the code:

 

 

public boolean keyChar(char key, int status, int time) {
      // Initialize Variables
      Field field = this.getLeafFieldWithFocus();
      Object object = field.getCookie();
      FieldCookie fieldCookie = null;
      if(object instanceof FieldCookie)
         fieldCookie = (FieldCookie)object;
      
      // Perform Checks
      if(key == Characters.ESCAPE) {
         this.myApp.invokeAndWait(new PopSomeScreen(this));
         return true;
      }
      else if(key == Characters.BACKSPACE) {
         if(fieldCookie != null) {
            if (fieldCookie.getType() == FieldCookie.TYPE_COMMENTS_BOX) {
               int totalLen = this.commentsBox.getText().length();
               if (totalLen == 1)
                  this.commentsBox.setText("");
               else if (totalLen > 1)
                  this.commentsBox.setText(this.commentsBox.getText().substring(0, totalLen - 1));
               updateCommentsBoxCharCounter();
               return true;
            } // end-if
         } // end-if
      } // end-elseif (key == Characters.BACKSPACE)
      else if (key == Characters.ENTER) {
         if (fieldCookie != null) {
            switch (fieldCookie.getType()) {
               case FieldCookie.TYPE_USERNAME_BOX:
                  setFocus(this.passwordBox, 0, 0, status, time);
                  return true;
               case FieldCookie.TYPE_PASSWORD_BOX:
                  setFocus(this.commentsBox, 0, 0, status, time);
                  return true;
               case FieldCookie.TYPE_COMMENTS_BOX:
                  this.commentsBox.setText(this.commentsBox.getText()+"\n");
                  updateCommentsBoxCharCounter();
                  return true;
               case FieldCookie.TYPE_SEND_BUTTON:
                  if (canSend()) {
                     myApp.invokeAndWait(new PopSomeScreen(this));
                     send();
                  } // end-if
                  return true;
               default:
                  return false;
            } // end-switch
         } // end if(object instanceof FieldCookie)
      } // end-else if (key == Characters.ENTER)
      else {
         // Absorb any other keys pressed
         if (fieldCookie != null) {
            if (fieldCookie.getType() == FieldCookie.TYPE_COMMENTS_BOX) {
               if(this.commentsBox.getTextLength() < TEXT_INPUT_MAX_CHARS) {
                  this.commentsBox.setText(this.commentsBox.getText() + key);
                  updateCommentsBoxCharCounter();
                  return true;
               }
            } // end-if
         } // end-if
      }
      updateCommentsBoxCharCounter();
      return false;
}

 

 

Developer
Posts: 282
Registered: ‎02-25-2010
My Device: curve,bold,storm
My Carrier: Verizon,AT&T

Re: EditField Problems within VerticalFieldManager related to keyChar

in your code for backspace i found 1 thing

 

if user scrolls the middle of text and he presses backspace

in that case the total length of text will always be greater than 1

and u have written:

 

 else if (totalLen > 1)
this.commentsBox.setText(this.commentsBox.getText().substring(0, totalLen - 1));

 

u r removing the contents from end of text not from the index where cursor is as u r doing substring from

0 to totalLength-1

 

 

 

Developer
Posts: 171
Registered: ‎08-20-2008
My Device: Not Specified

Re: EditField Problems within VerticalFieldManager related to keyChar

Have you tried using super.keyChar to absorb the key presses at the end?

keyChar
Developer
Posts: 282
Registered: ‎02-25-2010
My Device: curve,bold,storm
My Carrier: Verizon,AT&T

Re: EditField Problems within VerticalFieldManager related to keyChar

getCursorPosition()

 

u can also try the above method to get the cursor position and deleting the contents or doing substring accordingly

 

but i think it wil involve lots of code related to String manipulation

 

need to find a better way out

Developer
Posts: 88
Registered: ‎07-11-2009
My Device: Not Specified

Re: EditField Problems within VerticalFieldManager related to keyChar

Hey, super.keyChar() worked perfectly.