>我像这样覆盖方法。
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
构造函数字段,只需获得点击即可。 这决定了字段是使用单击事件,还是让它们传播到其他类进行处理。 但是,海报的代码已经在他的两种点击处理方法中返回了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。 在抬起用户的手指之前,单击不会注册。 但是,这是一件小事,不是这个问题的原因。
你Custom_ButtonField的父母是什么?
如果是按钮字段,则必须在构造函数中设置属性CONSUME_CLICK