【问题标题】:Click event is working outside the button点击事件在按钮外工作
【发布时间】:2012-03-27 05:29:24
【问题描述】:

我在屏幕的底部栏中放置了一个按钮。底栏基本上是一个带有背景图像的水平字段管理器。现在这个按钮(基本上是一个继续按钮)用于移动到下一个屏幕。但如果我在管理器中和按钮外单击,它将进入下一个屏幕。所以点击事件对总经理起作用,下面是代码:

HorizontalFieldManager hfmBtn = new HorizontalFieldManager(Field.FIELD_BOTTOM)
    {
        protected void sublayout(int nMaxWidth, int nMaxHeight) 
        {
            height = Bitmap.getBitmapResource("buttom_bar.png").getHeight();
            super.sublayout(nMaxWidth, nMaxHeight);
            setExtent(getPreferredWidth(), getPreferredHeight());
        }

        public int getPreferredHeight() 
        {
            return height;
        }

        public int getPreferredWidth() 
        {
            return screenWidth;
        }
    };
    hfmBtn.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("buttom_bar.png")));
    btnContinue = new CustomButtonImageField("Continue", Bitmap.getBitmapResource("button_normal.png"), Bitmap.getBitmapResource("button_hover.png"),Field.FIELD_VCENTER);
    btnContinue.setChangeListener(this);
    btnContinue.setMargin(10, 0, 0, screenWidth/3);
    hfmBtn.add(btnContinue);
    add(hfmBtn);

这里是点击事件:

    public void fieldChanged(Field field, int context) 
{
    if(field == btnContinue)
    {
        UiApplication.getUiApplication().pushScreen(new SendListScreen(platformContext,strItemList));
    }
}

请帮帮我。我正在用 BB Bold 9790 进行测试。

【问题讨论】:

  • 没有其他领域有焦点。所以默认焦点将在按钮中。
  • 一旦您进入按钮焦点,当您单击其他区域时,您将获得该按钮单击事件。但是当你专注于其他领域时,你不会遇到这个问题。所以添加一个 Focusable 字段进行测试,而不是尝试点击按钮 ..

标签: blackberry


【解决方案1】:

只需在添加按钮之前添加nullfield。并使其能够聚焦。那可行。

像这样添加nullField 然后添加button

hfmBtn.add(new nullField(Field.FOCUSABLE)); 
hfmBtn.add(btnContinue);
add(hfmBtn);

【讨论】:

    【解决方案2】:

    从以下链接中获取 PictureBackgroundButtonField.java

    PictureBackgroundButtonField

    然后试试这个示例代码:

    public class Abc extends MainScreen implements FieldChangeListener
    {   
    Bitmap bitmap=Bitmap.getBitmapResource("icon.png"),bitmapHover=Bitmap.getBitmapResource("iconHover.png");
    PictureBackgroundButtonField continueBtn;
    public Abc() 
    {       
        createGUI();
    }
    
    private void createGUI() 
    {   
        HorizontalFieldManager hr=new HorizontalFieldManager()
        {
            protected void sublayout(int maxWidth, int maxHeight) 
            {
                super.sublayout(Display.getWidth(),250);
                setExtent(Display.getWidth(),250);
            }
        };
        hr.add(new LabelField("Click The Below Button", Field.FOCUSABLE));
        continueBtn=new PictureBackgroundButtonField(bitmap.getWidth(), bitmap.getHeight(), Field.FIELD_HCENTER|Field.FOCUSABLE, bitmap, bitmapHover);
        continueBtn.setChangeListener(this);
        continueBtn.setMargin(50, 0, 0, 0);
        hr.add(continueBtn);
        hr.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
        add(hr);
    }
    
    public void fieldChanged(Field field, int context)
    {
        if(field==continueBtn)
        {
            Dialog.alert("Clicked");
        }
    }           
    }
    

    【讨论】:

      【解决方案3】:

      尝试添加您的自定义按钮,如下所示:

      hfmBtn.add(new PictureBackgroundButtonField(bitmap.getWidth(), bitmap.getHeight(), Field.FIELD_HCENTER|Field.FOCUSABLE, bitmap, bitmapHover) {
          protected boolean invokeAction(int action) {
              // code to be executed when button is pressed
              return true;
          }
      });
      

      【讨论】:

      • 我认为您是在告诉这样做: btnContinue = new CustomButtonImageField("Continue", Bitmap.getBitmapResource("button_normal.png"), Bitmap.getBitmapResource("button_normal.png"), Bitmap .getBitmapResource("button_hover.png")) { protected boolean invokeAction(int action) { UiApplication.getUiApplication().pushScreen(new SendListScreen(platformContext,strItemList));返回真; } }; hfmBtn.add(btnContinue);
      • 将“调用代码”更改为更有意义的“按下按钮时要执行的代码”
      • 如果我放的是黑莓本机按钮,而不是自定义按钮,它的行为方式相同..淡出 :)
      • 不要厌倦并在这里获得灵感:stackoverflow.com/questions/9093691/… 可能会改变您的所有实现...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 2011-12-30
      相关资源
      最近更新 更多