【问题标题】:Button activating when area outside of button is clicked (Java/Blackberry App)单击按钮外部区域时按钮激活(Java/Blackberry App)
【发布时间】:2013-01-20 19:21:45
【问题描述】:

我一直在为黑莓开发一个应用程序,它的屏幕上最初有两个按钮(btn1btn2)。现在我添加了第三个,但我遇到了一些困难 (btn3)。

最初btn1btn2 是并排的,在按钮外部单击,但在其下方会激活按钮……这是一个设计缺陷,但可能会被忽略。

但是,我需要在btn1 下方添加一个按钮,当我这样做时,发生了两件奇怪的事情:首先,即使我点击位于btn1 下方的btn3,焦点也会转移到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;
    }
}

【问题讨论】:

  • 请显示您创建按钮和其他字段的代码。
  • 我添加了在编辑中创建按钮的代码
  • 但是,我们仍然不知道CustomButtonField 是什么。这不是标准的黑莓类。因此,我们需要查看该类的构造函数,以及它继承自什么。
  • 将其添加到原始代码中

标签: java eclipse blackberry blackberry-simulator blackberry-eclipse-plugin


【解决方案1】:

当您第一次实现自定义 BlackBerry 按钮和字段时,这是一个很容易遇到的问题。首先,这里的问题是您的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 的示例之一

您可以使用BitmapButtonField 和它扩展的BaseButtonField 来实现相同的功能,并通过适当的触摸/点击处理。

在此期间,请查看该库中的其他一些 UI 类,因为您可能会发现它们非常有用。

【讨论】:

  • 我尝试了第一个选项,但变化不大...当我使用键盘上的触摸时,一切正常。但是屏幕上的触感还是一样的。不能正常工作。
  • @kemnet,你把上面的代码放在你的CustomButtonField 类中了吗?如果它不起作用,那么你做错了什么。我还要再说一遍,你应该用黑莓高级UI BitmapButtonField 类完全替换你的CustomButtonField 代码。
  • 非常感谢您的帮助
猜你喜欢
  • 2011-05-01
  • 1970-01-01
  • 2011-09-17
  • 2013-07-14
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多