黑莓中的自定义文本框

Posted

技术标签:

【中文标题】黑莓中的自定义文本框【英文标题】:CustomTextbox in Blackberry 【发布时间】:2012-07-04 08:07:04 【问题描述】:

嗨朋友们基本上我是一个安卓开发者。我是黑莓开发的新手。我需要创建带有图像按钮的自定义文本框。

在文本框的右上角,我想要小图像按钮,它是点击监听器,文本框字段应该是空的。

i 可以创建自定义文本框并在文本框内绘制位图。但我无法抓住图像按钮的焦点和侦听器。请帮帮我

请提供一些想法和示例。

我试过了……

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

我的问题是我不能在这里添加多个字段请帮助我..

【问题讨论】:

您正在添加两个字段,但您只能看到一个,因为在您的 Textbox 类中,管理器是使用标志 USE_ALL_HEIGHT 创建的。删除它,你应该能够看到两者。你也忘了添加按钮。 嗨史密斯,你能解释一下我删除了文本框类中的 USE_ALL_HEIGHT,而且我在自定义管理器类中添加了文本框和按钮字段.. 现在又看了一遍,好像忘记把hfm1加到vfm了。 我还添加了 hfm1....但我遇到了同样的问题...我只能查看一个带有按钮的文本框...看不到另一个..我不知道我在哪里制作的错误...你运行了我的代码... 尝试使用您的更改更新发布的代码,并显示您在哪里以及如何实例化文本框。与我在回答中推荐的方法不同,您在这里做了很多自定义开发。您可能需要调试一些错误。 【参考方案1】:

试试这个自定义类:

public class TextFieldWithClear extends HorizontalFieldManager 
protected HorizontalFieldManager hfmEditTextPanel;
protected LabelField lblEditText;
protected EditField textField;
protected MyImageButton bitmapFieldClear;
int mHeight;
int mWidth;
String mLabel;

public TextFieldWithClear(String label, int width, int height) 
    super(FOCUSABLE);

    Border border = BorderFactory
            .createSimpleBorder(new XYEdges(2, 2, 2, 2));
    this.setBorder(border);
    Background bg = BackgroundFactory.createSolidBackground(Color.WHITE);
    this.setBackground(bg);

    mWidth = width;
    mHeight = height;
    mLabel = label;

    lblEditText = new LabelField(mLabel) 
        protected void paint(Graphics graphics) 
            graphics.setColor(0x4B4B4B);
            super.paint(graphics);
        
    ;
    add(lblEditText);

    hfmEditTextPanel = new HorizontalFieldManager(FOCUSABLE
            | VERTICAL_SCROLL | VERTICAL_SCROLLBAR) 
        protected void sublayout(int maxWidth, int maxHeight) 
            maxWidth = mWidth - 30;
            maxHeight = mHeight;
            super.sublayout(maxWidth, maxHeight);
            setExtent(maxWidth, maxHeight);
        
    ;

    textField = new EditField() 
        // protected void layout(int width, int height)
        // 
        // width = mWidth - 50;
        // height=35;
        // super.layout(width, height);
        // //setExtent(width, height);
        // 
    ;
    hfmEditTextPanel.add(textField);
    add(hfmEditTextPanel);
    bitmapFieldClear = new MyImageButton(
            Bitmap.getBitmapResource("btn_delete_normal.png"),
            Bitmap.getBitmapResource("btn_delete_focused.png"));
    bitmapFieldClear.setChangeListener(buttonListener);
    add(bitmapFieldClear);


public String getText() 
    String value = "";
    if (textField.getText().length() > 0)
        value = textField.getText();
    return value;



public void setString(String value) 
    if (value != null) 
        textField.setText(value);
    


FieldChangeListener buttonListener = new FieldChangeListener() 
    public void fieldChanged(Field field, int context) 
        textField.clear(0);
        textField.setFocus();

    
;
public void onUndisplay()

    textField.setEditable(false);

public void onDisplay()

    textField.setEditable(true);

 

【讨论】:

【参考方案2】:

仅覆盖 EditField 并不容易,所以我会尝试这样做:

使用水平管理器(例如,HorizontalFieldManager 或其他自定义管理器,可能具有固定列宽。该管理器内部有两个字段:左侧是 EditField,右侧是自定义按钮字段。 为经理设置Background。 bg 将绘制绿色背景和蓝色边框。您可以使用缩放位图(查看BackgroundFactory.createBitmapBackground)。 创建一个新的EditField 子类,并重写它的paintBackground 方法,使它什么都不做。如果它不起作用,请尝试覆盖 paint 以便它只绘制文本。这是最棘手的部分。 使用交叉灰色圆圈图像创建自定义Buttonfield 子类。你可以阅读一个很好的教程来了解如何做到这一点here。您还可以在Advanced Ui Library 中使用已创建的BitmapButtonField。单击该按钮时,它将在 EditField 上调用 EditField.setText("")

【讨论】:

我已经编辑了我的问题......请看看并给我你的想法

以上是关于黑莓中的自定义文本框的主要内容,如果未能解决你的问题,请参考以下文章

清除文本字段黑莓

如何使用验证“唯一”访问 Laravel 中的自定义文本框名称?

仅在黑莓的文本框中更改特定文本的颜色

编辑文本框中提示的自定义位置。

WinForms 文本框的自定义插入符号

Flex:组合框控件的自定义项目渲染器截断文本