2

我一直在研究黑莓的應用程序,它最初在屏幕上有兩個按鈕(btn1btn2)。現在我添加了第三個,我遇到了一些困難(btn3)。點擊按鈕區域以外的按鈕(Java /黑莓應用程序)

原來btn1btn2並排,並點擊按鈕外,但低於它會激活按鈕......一個設計缺陷,但可以忽略。

不過,我需要添加以下btn1一個按鈕,當我做了兩個奇怪的事情發生:首先是,即使我點擊btn3這低於btn1,重點轉移到btn1btn1被調用。然後點擊btn2將焦點轉移到btn3並激活它。

我不完全確定爲什麼會發生這種情況,但我正在玩下面粘貼的代碼。任何小小的幫助表示讚賞。

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); 
      } 
     } 

    }; 

自定義按鈕現場

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

請在您創建按鈕和其他字段的位置顯示代碼。 – Nate

+0

我添加了代碼來編輯 – kemnet

+0

中的按鈕,但是我們仍然不知道CustomButtonField是什麼。這不是一個標準的黑莓班。所以,我們需要查看該類的構造函數以及它繼承的內容。 – Nate

回答

2

這是當你第一次實現定製的黑莓按鈕和領域有一個非常簡單的問題。首先,問題在於你的CustomButtonField類是一個從頭開始編寫的按鈕字段,它沒有正確地確定在其範圍(場內區域)內的哪個觸摸事件(或導航事件)爲。解決這個問題

一種方法是修改navigationClick()方法,並實現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; 
    } 

另一種選擇,那我真的建議,是一個樣品替換整個CustomButtonField類來自BlackBerry的Advanced UI library

您可以使用其擴展的BitmapButtonFieldBaseButtonField通過適當的觸摸/點擊處理實現相同的功能。

當您在那裏時,請查看該庫中的一些其他UI類,因爲您可能會發現它們非常有用。

+0

我嘗試了第一個選項,但沒有太多改變...當我使用鍵盤上的觸摸時,Everythig工作正常。但屏幕上的觸動仍然一樣。沒有正確的工作。 – kemnet

+0

@kemnet,你把上面的代碼放在你的'CustomButtonField'類中了嗎?如果它不起作用,你做錯了什麼。我還會再次說,你應該用黑莓高級UI BitmapButtonField類完全替代你的CustomButtonField代碼。 – Nate

+0

非常感謝您幫助 – kemnet