如何检测是否触摸或点击了字段?

时间:2012-07-23 09:11:29

标签: blackberry touch-event

我重写了这样的方法。

        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()按钮。

2 个答案:

答案 0 :(得分:2)

您不必使用CONSUME_CLICK构造函数字段,只需获取点击次数即可。这决定了字段消费点击事件,还是让它们传播到其他类来处理。但是,海报的代码已经在他的两个点击处理方法中返回true,这也意味着“我已经处理了这个点击...不要费心将它传递给其他Field类“See more on this here

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

如果Custom_ButtonField类是同一个you posted here,那么当我使用您的代码时,我可以获得点击。但是,我可以看到一个潜在的问题。您不显示Java导入。您的TouchEvent导入是否如此?

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

在BlackBerry框架中实际上有另一个TouchEvent类,如果你使用了错误的类,那么你创建了一个实际上不会覆盖基类touchEvent()的方法。使用Eclipse快捷方式放入导入很容易,但是可能会出错。

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

顺便说一下,

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

答案 1 :(得分:0)

Custom_ButtonField的父级是什么?

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