嗨朋友们基本上我是一个安卓开发者。我是黑莓开发的新手。我需要使用图像按钮创建自定义文本框。
在文本框的右上角,我想要小图像按钮,它是单击侦听器,文本框字段应该为空。
我可以创建自定义文本框并在文本框内绘制位图。但我无法抓住图像按钮的焦点和侦听器。请帮我
请给出一些想法和样本。
我试过这个...
MyApp.java 类:
import net.rim.device.api.ui.UiApplication;
公共类 MyApp 扩展 UiApplication {
public static void main(String[] args) {
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp() {
pushScreen(new MyScreen());
}
}
MyScreen.java 类:
public final class MyScreen extends MainScreen {
public MyScreen() {
super(Manager.NO_VERTICAL_SCROLL);
setTitle("MyTitle");
VerticalFieldManager vfm = new VerticalFieldManager(
Manager.USE_ALL_HEIGHT | Manager.USE_ALL_HEIGHT);
HorizontalFieldManager hfm = new HorizontalFieldManager(
Manager.USE_ALL_WIDTH);
HorizontalFieldManager hfm1 = new HorizontalFieldManager(
Manager.USE_ALL_WIDTH);
customManager ctm = new customManager(Manager.USE_ALL_WIDTH);
customManager ctm1 = new customManager(Manager.USE_ALL_WIDTH);
hfm.add(ctm);
hfm1.add(ctm1);
vfm.add(hfm);
vfm.add(hfm1);
add(vfm);
}
}
customManager.java 类:
public class customManager extends Manager implements FieldChangeListener {
private Textbox txt;
private Closebtn cls;
Bitmap bitmap;
protected customManager(long style) {
super(style);
// My Coustem TextBOX
txt = new Textbox(300, 100);
// My Coustem Button
cls = new Closebtn();
cls.setChangeListener(this);
add(txt);
add(cls);
}
protected void sublayout(int width, int height) {
setPositionChild(getField(0), 10, 10);
layoutChild(getField(0), getField(0).getPreferredWidth(), getField(0)
.getPreferredHeight());
setPositionChild(getField(1),
getField(0).getWidth() - (getField(1).getWidth()), getField(0)
.getHeight() / 2 - getField(1).getHeight() / 2);
layoutChild(getField(1), getField(1).getWidth(), getField(1)
.getHeight());
setExtent(width, height);
}
public void fieldChanged(Field field, int context) {
txt.setText("");
}
}
Textbox.java 类:
public class Textbox extends Manager {
private int managerWidth;
private int managerHeight;
private int arcWidth;
private VerticalFieldManager vfm = new VerticalFieldManager(
NO_VERTICAL_SCROLL | USE_ALL_WIDTH );
private EditField editField;
private Bitmap bagBitmap;
Textbox(int width, int height, long style) {
super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
managerWidth = width;
managerHeight = height;
long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
if (innerStyle == 0) {
innerStyle = FOCUSABLE;
}
editField = new EditField("", "", 10, innerStyle);
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; // make it even
EncodedImage en = EncodedImage.getEncodedImageResource("_text.png");
bagBitmap = Util.getScaledBitmapImage(en, width, height);
add(vfm);
vfm.add(editField);
}
public void setFont(Font font) {
super.setFont(font);
editField.setFont(font);
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
updateLayout();
}
Textbox(int width, int height) {
this(width, height, 0L);
}
public String getText() {
return editField.getText();
}
public void setText(String newText) {
editField.setText(newText);
}
public int getPreferredWidth() {
return managerWidth;
}
public int getPreferredHeight() {
return managerHeight;
}
protected void sublayout(int w, int h) {
if (managerWidth == 0) {
managerWidth = w;
}
if (managerHeight == 0) {
managerHeight = h;
}
int actWidth = Math.min(managerWidth, w);
int actHeight = Math.min(managerHeight, h);
layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth);
setPositionChild(vfm, arcWidth / 2, arcWidth / 2);
setExtent(actWidth, actHeight);
}
protected void paint(Graphics g) {
g.drawBitmap(0, 0, getWidth(), getHeight(), bagBitmap, 0, 0);
super.paint(g);
}
}
Closebtn.java 类:
public class Closebtn extends Field {
private Bitmap bitmap;
public Closebtn() {
super(Manager.FOCUSABLE);
EncodedImage en = EncodedImage.getEncodedImageResource("close.png");
bitmap = Util.getScaledBitmapImage(en, 50, 50);
}
protected void layout(int width, int height) {
setExtent(bitmap.getWidth(), bitmap.getHeight());
}
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
bitmap, 0, 0);
}
protected void onFocus(int direction) {
bitmap = bitmap;
}
protected void onUnfocus() {
bitmap = bitmap;
}
protected boolean keyChar(char character, int status, int time) {
if (character == Characters.ENTER) {
clickButton();
return true;
}
return super.keyChar(character, status, time);
}
protected boolean navigationClick(int status, int time) {
clickButton();
return true;
}
protected boolean trackwheelClick(int status, int time) {
clickButton();
return true;
}
protected boolean invokeAction(int action) {
switch (action) {
case ACTION_INVOKE: {
clickButton();
return true;
}
}
return super.invokeAction(action);
}
public void setDirty(boolean dirty) {
}
public void setMuddy(boolean muddy) {
}
public void clickButton() {
fieldChangeNotify(0);
}
}
Util.java 类:
public class Util {
public static Bitmap getScaledBitmapImage(EncodedImage image, int width,
int height) {
if (image == null) {
return null;
}
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}
}
我的问题是我不能在这里添加多个字段请帮助我..