0

我重写了这样的方法。

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

这里是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);
}
}

但是,按钮无法执行推屏,只能setfocus()在按钮上执行。

4

2 回答 2

2

您不必使用CONSUME_CLICK构造函数字段,只需获得点击。这决定了该字段是否使用点击事件,或者让它们传播到其他类来处理。但是,发帖人的代码已经true在他的两个点击处理方法中返回,这也意味着“我已经处理了这个点击......不要费心将它传递给其他Field类”在此处查看更多信息

正如艾伦在评论中所说,他已经在使用CONSUME_CLICK,所以这绝对不是问题。

如果该Custom_ButtonField课程与您在此处发布的课程相同,那么当我使用您的代码时,我就能很好地获得点击。但是,我可以看到一个潜在的问题。您没有显示您的 Java 导入。您的TouchEvent导入看起来像这样吗?

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

在 BlackBerry 框架中实际上还有另一个TouchEvent类,如果您使用了错误的类,那么您创建的方法实际上并没有覆盖基类touchEvent()。使用 Eclipse 快捷方式来输入您的导入很容易,但也有可能输入错误。

但是,我认为如果您这样做,Eclipse 应该会显示一个警告,即touchEvent()永远不会调用不正确的版本。

编辑:顺便说一句,我通常在事件上触发我的点击处理代码TouchEvent.UNCLICK,而不是在TouchEvent.CLICK. 我认为这会带来更好的 UI。在用户的手指被抬起之前,点击不会注册。但是,这是一件小事,并不是造成这个问题的原因。

于 2012-07-23T13:19:03.277 回答
0

您的 Custom_ButtonField 的父级是什么?

如果是buttonField,则要在构造函数中设置属性CONSUME_CLICK

于 2012-07-23T12:06:47.747 回答