如何检测场是否被触摸或点击? [英] How to detect whether field is touched or clicked?

查看:178
本文介绍了如何检测场是否被触摸或点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我覆盖的方法是这样的。

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

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

推荐答案

您不必使用 CONSUME_CLICK 构造领域,只是获取点击。这决定是否领域的消耗点击事件,或者让他们传播给其他的类来处理。但是,海报的code已经返回真正在他的两个click处理方法,这也意味着的我已经处理了这个点击...别'吨懒得把它传递给其他字段的。 <一href=\"http://supportforums.blackberry.com/t5/Java-Development/ButtonField-CONSUME-CLICK-what-is-it-for/td-p/190434\"相对=nofollow>查看更多关于这个这里

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

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

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

如果在 Custom_ButtonField 类是同一个的你贴这里,那么我能当我使用你的code获得点击就好了。然而,有我可以看到一个潜在的问题。你不显示您的Java进口。请问您的的TouchEvent 导入这个样子的?

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;

有是在BlackBerry框架实际上是另一个的TouchEvent 类,如果你使用了错误的一个,那么你已经创建了一个实际上并没有覆盖基类的方法的TouchEvent()。它易于使用Eclipse快​​捷方式放到你的进口,但它可能得到错误的。

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.

我想,如果你这样做,不过,Eclipse应该会显示一个警告,的TouchEvent的版本不正确()永远不会被调用。

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

编辑:顺便说一句,我通常会触发的 TouchEvent.UNCLICK 事件我的点击处理code,而不是 TouchEvent.CLICK 。我认为,让一个更好的UI。点击不注册,直到用户的手指已经解除。但是,这是一个次要的事情,而不是这个问题的原因。

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.

这篇关于如何检测场是否被触摸或点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆