我一直在为黑莓开发一个应用程序,它最初在屏幕上有两个按钮(btn1
和)。btn2
现在我添加了第三个,但我遇到了一些困难(btn3
)。
最初是并排的,在按钮外部单击,但在其下方会激活按钮……这是一个设计缺陷,但可能会被忽略btn1
。btn2
但是,我需要在下面添加一个按钮btn1
,当我这样做时,发生了两件奇怪的事情:首先,即使我点击btn3
了 BELOW btn1
,焦点也会转移到btn1
并被btn1
调用。单击btn2
将焦点转移到btn3
并激活它。
我不完全确定为什么会这样,但我正在玩弄下面粘贴的代码。任何一点帮助表示赞赏。
btn1 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled_1a.png"), Bitmap.getBitmapResource("button-normal_2.png"));
btn2 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled_3.png"), Bitmap.getBitmapResource("button-normal_4.png"));
btn3 = new CustomButtonField("", Bitmap.getBitmapResource("button-disabled5.png"), Bitmap.getBitmapResource("button-normal_6.png"));
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("background.png"));
HorizontalFieldManager vfm = new HorizontalFieldManager(){
public int getPreferredHeight() {
// TODO Auto-generated method stub
return Display.getHeight();
}
public int getPreferredWidth() {
// TODO Auto-generated method stub
return Display.getWidth();
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
int count = getFieldCount();
for(int i = 0 ; i < count ; i++ ){
Field f = getField(i);
if(f == btn1 ){
setPositionChild(f, (getPreferredWidth() >> 1) - f.getPreferredWidth(), getPreferredHeight()>>1);
layoutChild(f, getPreferredWidth(), getPreferredHeight());
}else if (f == btn2 ){
setPositionChild(f, (getPreferredWidth() >> 1) +30, getPreferredHeight()>>1);
layoutChild(f, getPreferredWidth(), getPreferredHeight());
}else if (f == lblName ){
setPositionChild(f, 30, getPreferredHeight()>>1 - btnLicense.getPreferredHeight());
layoutChild(f, ( getPreferredWidth() * 3 ) >> 2, getPreferredHeight());
}else if (f == btn3 ){
setPositionChild(f, (getPreferredWidth() >> 1) - f.getPreferredWidth() -0 , getPreferredHeight()- getPreferredHeight()+280);
layoutChild(f, getPreferredWidth(), getPreferredHeight());
}
}
setExtent(getPreferredWidth(),getPreferredHeight());
}
public void subpaint(Graphics graphics){
int count = getFieldCount();
for(int i = 0 ; i < count ; i++ ){
net.rim.device.api.ui.Field f = getField(i);
paintChild(graphics,f);
}
}
};
自定义按钮字段
package com.app.ui.component;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class CustomButtonField extends Field {
/** To set background image for button field */
private Bitmap bkImage;
/** To set Focus image for button field */
private Bitmap bkFocusImage;
/** int value for Field Width */
private int fieldWidth;
/** int value for Field Height */
private int fieldHeight;
/** Text to write on Button */
private String text;
/** Text Color on Button */
private int textColor = Color.WHITE;
/** Default Font for Button */
private Font defaultFont = Font.getDefault();
/**
* Constructor with
* @param text
* @param image
* @param focusImage
*/
public CustomButtonField (String text, Bitmap image, Bitmap focusImage) {
this(text, image, focusImage, 0);
}
/**
* Constructor with
* @param text
* @param image
* @param focusImage
* @param style
*/
public CustomButtonField(String text, Bitmap image, Bitmap focusImage, long style) {
super(Field.FOCUSABLE | style);
this.text = text;
bkImage = image;
this.bkFocusImage = focusImage;
fieldHeight = bkImage.getHeight();
fieldWidth = bkImage.getWidth();
}
/**
* To get the exact width needed by the field borderWidth - used to show the
* width of focused rectangle around the button
*/
public int getPreferredWidth() {
return fieldWidth;
}
/**
* To get the exact width needed by the field borderHeight - used to show
* the height of focused rectangle around the button
*/
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
/**
* To set the background according to focused state of the field
*/
protected void drawFocus(Graphics graphics, boolean flag) {
graphics.setFont(defaultFont);
if (bkFocusImage != null) {
graphics.drawBitmap((getPreferredWidth() - bkFocusImage.getWidth()) / 2,(getPreferredHeight() - bkFocusImage.getHeight()) / 2,
bkFocusImage.getWidth(), bkFocusImage.getHeight(),bkFocusImage, 0, 0);
}
graphics.setColor(Color.WHITE);
int textWidth = defaultFont.getAdvance(text);
graphics.drawText(text, (fieldWidth - textWidth) / 2,(fieldHeight - defaultFont.getHeight()) / 2);
}
protected void paint(Graphics graphics) {
graphics.setFont(defaultFont);
if (bkImage != null) {
graphics.drawBitmap((getPreferredWidth() - bkImage.getWidth()) / 2,(getPreferredHeight() - bkImage.getHeight()) / 2,
bkImage.getWidth(), bkImage.getHeight(), bkImage, 0, 0);
}
graphics.setColor(textColor);
int color = (isEnabled())?Color.BLACK:Color.DARKGRAY;
graphics.setColor(color);
int textWidth = defaultFont.getAdvance(text);
graphics.drawText(text, (fieldWidth - textWidth) / 2,(fieldHeight - defaultFont.getHeight()) / 2);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
}