2

Je travaille sur une application pour blackberry, qui avait initialement deux boutons (btn1 et btn2) sur l'écran. Maintenant, j'ai ajouté un troisième et je rencontre quelques difficultés (btn3). Côte à côte, et en cliquant à l'extérieur du bouton, mais en dessous, le bouton ... activerait un défaut de conception, mais pourrait être négligé.Bouton activant lorsque la zone en dehors du bouton est cliquée (Java/Blackberry App)

Cependant, je dois ajouter un bouton ci-dessous btn1 et quand je l'ai fait deux choses étranges se sont produites: La première est que, même si je clique btn3 ce qui est inférieur btn1, le focus passe à btn1 et btn1 est appelé. Et en cliquant sur btn2, les changements se concentrent sur btn3 et il est activé. Je ne suis pas entièrement sûr pourquoi cela se produit mais je joue avec le code collé ci-dessous. Toute petite aide est appréciée.

btn1 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled_1a.png"), Bitmap.getBitmapResource("button-normal_2.png"));  
    btn2 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled_3.png"), Bitmap.getBitmapResource("button-normal_4.png")); 
    btn3 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled5.png"), Bitmap.getBitmapResource("button-normal_6.png")); 

    Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("background.png")); 

    HorizontalFieldManager vfm = new HorizontalFieldManager(){ 
     public int getPreferredHeight() { 
      // TODO Auto-generated method stub 
      return Display.getHeight(); 
     } 

     public int getPreferredWidth() { 
      // TODO Auto-generated method stub 
      return Display.getWidth(); 
     } 

     protected void sublayout(int maxWidth, int maxHeight) { 
      // TODO Auto-generated method stub 
      int count = getFieldCount(); 
      for(int i = 0 ; i < count ; i++){ 
       Field f = getField(i); 

      if(f == btn1){ 
       setPositionChild(f, (getPreferredWidth() >> 1) - f.getPreferredWidth(), getPreferredHeight()>>1); 
        layoutChild(f, getPreferredWidth(), getPreferredHeight()); 
       }else if (f == btn2){ 
        setPositionChild(f, (getPreferredWidth() >> 1) +30, getPreferredHeight()>>1); 
        layoutChild(f, getPreferredWidth(), getPreferredHeight()); 
       }else if (f == lblName){ 
        setPositionChild(f, 30, getPreferredHeight()>>1 - btnLicense.getPreferredHeight()); 
        layoutChild(f, (getPreferredWidth() * 3) >> 2, getPreferredHeight()); 
       }else if (f == btn3){ 

        setPositionChild(f, (getPreferredWidth() >> 1) - f.getPreferredWidth() -0 , getPreferredHeight()- getPreferredHeight()+280); 
        layoutChild(f, getPreferredWidth(), getPreferredHeight()); 
       } 

      } 
      setExtent(getPreferredWidth(),getPreferredHeight()); 
     } 

     public void subpaint(Graphics graphics){ 
      int count = getFieldCount(); 
      for(int i = 0 ; i < count ; i++){ 
       net.rim.device.api.ui.Field f = getField(i); 
       paintChild(graphics,f); 
      } 
     } 

    }; 

Bouton champ personnalisé

package com.app.ui.component; 

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.Font; 
import net.rim.device.api.ui.Graphics; 

public class CustomButtonField extends Field { 

    /** To set background image for button field */ 
    private Bitmap bkImage; 

    /** To set Focus image for button field */ 
    private Bitmap bkFocusImage; 

    /** int value for Field Width */ 
    private int fieldWidth; 

    /** int value for Field Height */ 
    private int fieldHeight; 

    /** Text to write on Button */ 
    private String text; 

    /** Text Color on Button */ 
    private int textColor = Color.WHITE; 

    /** Default Font for Button */ 
    private Font defaultFont = Font.getDefault(); 

    /** 
    * Constructor with 
    * @param text 
    * @param image 
    * @param focusImage 
    */ 
    public CustomButtonField (String text, Bitmap image, Bitmap focusImage) { 
     this(text, image, focusImage, 0); 
    } 

    /** 
    * Constructor with 
    * @param text 
    * @param image 
    * @param focusImage 
    * @param style 
    */ 
    public CustomButtonField(String text, Bitmap image, Bitmap focusImage, long style) { 
     super(Field.FOCUSABLE | style); 
     this.text = text; 
     bkImage = image; 
     this.bkFocusImage = focusImage; 
     fieldHeight = bkImage.getHeight(); 
     fieldWidth = bkImage.getWidth(); 
    } 

    /** 
    * To get the exact width needed by the field borderWidth - used to show the 
    * width of focused rectangle around the button 
    */ 
    public int getPreferredWidth() { 
     return fieldWidth; 
    } 

    /** 
    * To get the exact width needed by the field borderHeight - used to show 
    * the height of focused rectangle around the button 
    */ 
    public int getPreferredHeight() { 
     return fieldHeight; 
    } 

    protected void layout(int width, int height) { 
     setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    /** 
    * To set the background according to focused state of the field 
    */ 
    protected void drawFocus(Graphics graphics, boolean flag) { 
     graphics.setFont(defaultFont); 
     if (bkFocusImage != null) { 
      graphics.drawBitmap((getPreferredWidth() - bkFocusImage.getWidth())/2,(getPreferredHeight() - bkFocusImage.getHeight())/2, 
        bkFocusImage.getWidth(), bkFocusImage.getHeight(),bkFocusImage, 0, 0); 
     } 
     graphics.setColor(Color.WHITE); 
     int textWidth = defaultFont.getAdvance(text); 
     graphics.drawText(text, (fieldWidth - textWidth)/2,(fieldHeight - defaultFont.getHeight())/2); 
    } 

    protected void paint(Graphics graphics) { 
     graphics.setFont(defaultFont); 
     if (bkImage != null) { 
      graphics.drawBitmap((getPreferredWidth() - bkImage.getWidth())/2,(getPreferredHeight() - bkImage.getHeight())/2, 
        bkImage.getWidth(), bkImage.getHeight(), bkImage, 0, 0); 
     } 
     graphics.setColor(textColor); 
     int color = (isEnabled())?Color.BLACK:Color.DARKGRAY; 
     graphics.setColor(color); 

     int textWidth = defaultFont.getAdvance(text); 
     graphics.drawText(text, (fieldWidth - textWidth)/2,(fieldHeight - defaultFont.getHeight())/2); 
    } 

    protected boolean navigationClick(int status, int time) { 
     fieldChangeNotify(0); 
     return true; 
    } 
} 
+0

veuillez indiquer le code dans lequel vous créez vos boutons et autres champs. – Nate

+0

J'ai ajouté le code pour créer le bouton dans une édition – kemnet

+0

mais, nous ne savons toujours pas ce qu'est 'CustomButtonField'. ce n'est pas une classe BlackBerry standard. donc, nous aurions besoin de voir le constructeur pour cette classe, ainsi que ce dont il hérite. – Nate

Répondre

2

Ce problème est assez facile d'avoir quand vous êtes d'abord la mise en œuvre des boutons personnalisés avec BlackBerry et les champs. Tout d'abord, le problème ici est que votre classe CustomButtonField, qui est un champ de bouton écrit à partir de zéro, ne détermine pas correctement quels événements tactiles (ou événements de navigation) sont dans son étendue (à l'intérieur de la zone du champ).

Une façon de résoudre ce problème est de modifier votre méthode navigationClick(), et mettre en œuvre la méthode touchEvent():

protected boolean touchEvent(TouchEvent message) { 
     int x = message.getX(1);   
     int y = message.getY(1);   
     if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) { 
     // Outside the field    
     return false;  
     }   

     switch(message.getEvent()) {     
     case TouchEvent.UNCLICK:     
     fieldChangeNotify(0);    
     return true;   
     }   
     return super.touchEvent(message);  
    } 

    protected boolean navigationClick(int status, int time) { 
     if (status != 0) {  // you did not have this check 
     fieldChangeNotify(0); 
     } 
     return true; 
    } 

Une autre option, que je recommande en fait, est de remplacer toute la classe CustomButtonField avec l'un des échantillons à partir du BlackBerry Advanced UI library

Vous pouvez utiliser le BitmapButtonField et le BaseButtonField qu'il étend, pour obtenir la même fonctionnalité, avec une manipulation tactile/clic appropriée.

Pendant que vous êtes là, jetez un oeil à certaines des autres classes d'interface utilisateur de cette bibliothèque, car vous les trouverez probablement très utiles.

+0

J'ai essayé les premières options mais pas beaucoup a changé ... Everythig fonctionne très bien lorsque j'utilise les touches du clavier. Mais les touches sur l'écran restent les mêmes. Ne fonctionne pas correctement. – kemnet

+0

@kemnet, avez-vous mis le code ci-dessus dans votre classe 'CustomButtonField'? Si cela ne fonctionne pas, vous avez fait quelque chose de mal. Je dirai aussi, encore une fois, que vous devriez remplacer complètement votre code 'CustomButtonField' avec la classe blackberry advancedUI' BitmapButtonField'. – Nate

+0

Merci beaucoup d'aider – kemnet

Questions connexes