Click事件在按钮外部工作

时间:2012-03-27 05:29:25

标签: blackberry

我在屏幕的底栏放了一个按钮。底栏基本上是具有背景图像的水平场管理器。现在,此按钮(基本上是一个继续按钮)用于移动到下一个屏幕。但如果我点击管理员并在按钮外面,它将进入下一个屏幕。所以click事件正在对总经理起作用,下面是代码:

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。

3 个答案:

答案 0 :(得分:7)

在添加按钮之前添加nullfield。并使其成为焦点。那可行。

像这样添加nullField,然后添加button

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

从以下链接中获取 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");
    }
}           
}