我重寫了這樣的方法。如何檢測字段是否被觸摸或點擊?
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()
上的按鈕。
您可以發佈您充分Custom_ButtonField類?我知道你還有另一個問題,但我不確定你是否從那時起進行過修改。謝謝。 – Nate 2012-07-24 08:04:26