0

I override the method like this.

        newsbtn = new Custom_ButtonField(news, newsactive, newsactive) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().pushScreen(
                        new Menu_PopupMenu(position));
                return true;
            }

            protected boolean touchEvent(TouchEvent message) {
                int eventCode = message.getEvent();
                if (eventCode == TouchEvent.UNCLICK){
                    Main.getUiApplication().pushScreen(
                            new Menu_PopupMenu(position));
                }
                return true;
            }
        };

        add(newsbtn);

Here is the Custom_ButtonField

public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;

int mWidth;
int mHeight;

private int color = -1;
String text;

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color, long style) {
    super(style);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

public void setText(String text){
    this.text = text;
    invalidate();
}

public String getText(){
    return text;
}

public void setColor(int color){
    this.color = color;
}

protected void onFocus(int direction) {     
    super.onFocus(direction);
    color = 0x540604;
    this.invalidate();
}

protected void onUnfocus() {
    super.onUnfocus();
    color = Color.WHITE;
    this.invalidate();
}

protected void paint(Graphics graphics) {
    int fontcontent;
    if (Display.getWidth() > 480)
        fontcontent = 28;
    else if (Display.getWidth() < 481 && Display.getWidth() > 320)
        fontcontent = 23;
    else
        fontcontent = 18;

    Bitmap bitmap = null;
    switch (getVisualState()) {
    case VISUAL_STATE_NORMAL:
        bitmap = mNormal;
        break;
    case VISUAL_STATE_FOCUS:
        bitmap = mFocused;
        break;
    case VISUAL_STATE_ACTIVE:
        bitmap = mActive;
        break;
    default:
        bitmap = mNormal;
    }
    setBackground(BackgroundFactory.createBitmapBackground(bitmap));
    graphics.setFont(Font.getDefault().derive(Font.PLAIN, fontcontent));
    graphics.setColor(color);
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault()
            .getAdvance(text)) / 2, ((mNormal.getHeight() - Font
            .getDefault().getHeight()) / 2) + 10, DrawStyle.HCENTER
            | DrawStyle.VCENTER);
}

public int getPreferredWidth() {
    return mWidth;
}

public int getPreferredHeight() {
    return mHeight;
}

protected void layout(int width, int height) {
    setExtent(mWidth, mHeight);
}
}

However, the button unable to perform push screen and only setfocus() on the button.

Nate
  • 31,017
  • 13
  • 83
  • 207
Alan Lai
  • 1,296
  • 2
  • 12
  • 16
  • Can you post your full Custom_ButtonField class? I know there's another question of your that had it, but I'm not sure if you made changes since then. Thanks. – Nate Jul 24 '12 at 08:04

2 Answers2

2

You don't have to use the CONSUME_CLICK constructor field, just to get clicks. That determines whether or not the field consumes click events, or lets them propagate to other classes to handle. But, the poster's code is already returning true in his two click handling methods, which also means "I've already handled this click ... don't bother passing it to other Field classes". See more on this here

And as Alan said in his comment, he was already using CONSUME_CLICK, so that's definitely not the problem.

If the Custom_ButtonField class is the same one you posted here, then I am able to get clicks just fine when I use your code. However, there's one potential problem I could see. You don't show your Java imports. Does your TouchEvent import look like this?

import net.rim.device.api.ui.TouchEvent;

There's actually another TouchEvent class in the BlackBerry frameworks, and if you used the wrong one, then you've created a method that doesn't actually override the base class touchEvent(). It's easy to use the Eclipse shortcut to put in your imports, but it's possible to get the wrong one.

I think if you do this, though, Eclipse should show a warning that the incorrect version of touchEvent() is never called.

Edit: by the way, I usually trigger my click handling code on the TouchEvent.UNCLICK event, not on TouchEvent.CLICK. I think that makes for a better UI. The click doesn't register until the user's finger has been lifted. But, that's a minor thing, and isn't the reason for this problem.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • When I click the button, It only changed the image and `focus` only but not perform `pushscreen` – Alan Lai Jul 24 '12 at 02:20
  • I just used `TouchEvent.UNCLICK`, it works well for first time which was push to `Main.getUiApplication().pushScreen( new Menu_PopupMenu(position));` but when i click other button, it should work itself function but it still go `Main.getUiApplication().pushScreen( new Menu_PopupMenu(position));` – Alan Lai Jul 24 '12 at 02:47
  • Even I click the button, but it will perform the function that the current focusing object's function. – Alan Lai Jul 24 '12 at 03:00
  • @AlanLai, so it works **one** time, and then does not work the second time? Also, are you testing this on a touchscreen device, or no? – Nate Jul 24 '12 at 04:14
  • yup, bold 9900. Now more worst, touch also no response. – Alan Lai Jul 24 '12 at 09:54
  • @AlanLai, I'm a little confused. Does it work once, or not at all? Is there a difference for the navigation click, vs. the touch event? I'm shutting down for tonight, but I can look at this again tomorrow. – Nate Jul 25 '12 at 10:19
  • [here](http://stackoverflow.com/questions/11647008/unable-to-perform-another-buttons-function-if-current-button-is-focusing) I started new question but slightly difference code – Alan Lai Jul 25 '12 at 12:10
0

What is the parent of your Custom_ButtonField ?

If it is a buttonField, you have to set the attribute CONSUME_CLICK in the constructor

Benoit
  • 1,922
  • 16
  • 25