如何从黑莓中动态生成的编辑字段获取价值

发布于 2024-12-19 06:31:58 字数 284 浏览 0 评论 0原文

我需要在运行时动态生成 ui 组件。我能够生成组件,但我的问题是如何获取动态生成字段中输入的值?

我正在做这样的事情。

    final HorizontalFieldManager hfm = new HorizontalFieldManager();

    for (int i=0; i<5; i++)
    {
        hfm.add(new EditField());
    }

有没有办法设置字段的标签,然后我们可以通过标签找到控制?

I need to generate ui component dynamically at runtime. I am able to generate the component, but my problem is how to get value entered in dynamically generated field?

I am doing something like this.

    final HorizontalFieldManager hfm = new HorizontalFieldManager();

    for (int i=0; i<5; i++)
    {
        hfm.add(new EditField());
    }

Is there any way to set tag of field and later we could find control by tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘑菇王子 2024-12-26 06:31:58

使用 Field.setCookie(Object)Field.getCookie () 在字段上设置和检索 cookie。您可以使用它来按照您想要的方式匹配编辑字段。

Use Field.setCookie(Object) and Field.getCookie() to set and retrieve a cookie on the field. You can use this to match up the edit field the way you want.

花心好男孩 2024-12-26 06:31:58

据我所知,没有办法为 EditField 设置唯一的 id。也许你可以使用它的子类,并实现你自己独特的 id 机制。
或者,由于 Horizo​​ntalFieldManager 仅包含 EditField,因此您可以按位置获取字段,并将其转换为 EditField。像这样:

Field f = hfm.getField(index);
EditField ef = (EditField)f;

更新:

<代码>
公共类 MyEditField 扩展 EditField {

private int _id;

public MyEditField(int id) {
    _id = id;
}

public int getID() {
    return _id;
}

}

类 MyHfm 扩展 Horizo​​ntalFieldManager {

    //This is a cache, which holds all EditFields.

私有 IntHashtable _editfields = new IntHashtable();

    public EditField getById(int id) {
        EditField ef = (EditField)_editfields.get(id);
        return ef;
}

    public void add(Field f) {
        super.add(f);
        if (f instanceof MyEditField) {
            _editfields.put(((MyEditField)f).getID(), f);
        }
    }

    public void delete(Field f) {
        super.delete(f);
        if (f instanceof MyEditField) {
            _editfields.remove(((MyEditField)f).getID());
        }
    }

<代码>}

然后你可以这样做:

    MyHfm hfm = new MyHfm();
    hfm.add(new MyEditField(0));
    hfm.add(new MyEditField(1));
    //get the field with id
    EditField f = hfm.getById(1);

还有一些其他的删除/添加方法,记得处理它们内部的缓存。

As far as I know, there is no way to set a unique id for an EditField. Maybe you can use a sub class of it, and implement your own unique id mechanism.
Or, as your HorizontalFieldManager holds nothing but EditField, you can get a field by position, and cast it to EditField. Like this:

Field f = hfm.getField(index);
EditField ef = (EditField)f;

UPDATE:


public class MyEditField extends EditField {

private int _id;

public MyEditField(int id) {
    _id = id;
}

public int getID() {
    return _id;
}

}

class MyHfm extends HorizontalFieldManager {

    //This is a cache, which holds all EditFields.

private IntHashtable _editfields = new IntHashtable();

    public EditField getById(int id) {
        EditField ef = (EditField)_editfields.get(id);
        return ef;
}

    public void add(Field f) {
        super.add(f);
        if (f instanceof MyEditField) {
            _editfields.put(((MyEditField)f).getID(), f);
        }
    }

    public void delete(Field f) {
        super.delete(f);
        if (f instanceof MyEditField) {
            _editfields.remove(((MyEditField)f).getID());
        }
    }

}

And then you can do like this:

    MyHfm hfm = new MyHfm();
    hfm.add(new MyEditField(0));
    hfm.add(new MyEditField(1));
    //get the field with id
    EditField f = hfm.getById(1);

There are some other delete/add methods, remember to handle the cache inside them.

江南烟雨〆相思醉 2024-12-26 06:31:58
EditField[] ef = new EditField[size];//at global in class

//做一些事情

VerticalFieldManager vfm = new VerticalFieldManager();
for(int i=0 ;i<size ;i++)
{
    HorizontalFieldManager hfm = new HorizontalFieldManager();
    ef[i] = new EditField();
    hfm.add(ef[i]);
    vfm.add(hfm);
}
add(vfm);

//完成创建UI

//开始从EditField获取值

public void fieldChanged(Field field, int context) {
    super.fieldChanged(field, context);
    for(int i=0;i<size;i++)
    {
        if (field.equals(ef[i]) {
            system.out.println("Value from editfield :- " + ef[i].getText());
        }
    }
}
EditField[] ef = new EditField[size];//at global in class

//Do something

VerticalFieldManager vfm = new VerticalFieldManager();
for(int i=0 ;i<size ;i++)
{
    HorizontalFieldManager hfm = new HorizontalFieldManager();
    ef[i] = new EditField();
    hfm.add(ef[i]);
    vfm.add(hfm);
}
add(vfm);

//complete Creating UI

// Start getting value from EditField

public void fieldChanged(Field field, int context) {
    super.fieldChanged(field, context);
    for(int i=0;i<size;i++)
    {
        if (field.equals(ef[i]) {
            system.out.println("Value from editfield :- " + ef[i].getText());
        }
    }
}
枕梦 2024-12-26 06:31:58

您可以像这样尝试

Editfield ef = new Editfield();
add(ef);
ef.setcookie("Here you can set object with specific value");
ef.setchangelistner(this);

并在字段更改列表器功能中

public void fieldChanged(Field field, int context) {
    object ob = field.getcookie();
    // compare this ob and give appropriate actionss
}

You can Try like this

Editfield ef = new Editfield();
add(ef);
ef.setcookie("Here you can set object with specific value");
ef.setchangelistner(this);

and in field change listner function

public void fieldChanged(Field field, int context) {
    object ob = field.getcookie();
    // compare this ob and give appropriate actionss
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文