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

时间:2011-12-03 15:23:36

标签: user-interface blackberry dynamic-ui blackberry-editfield

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

我正在做这样的事情。

    final HorizontalFieldManager hfm = new HorizontalFieldManager();

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

有没有办法设置字段标记,以后我们可以通过标记找到控件?

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

更新:

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

private int _id; public MyEditField(int id) { _id = id; } public int getID() { return _id; }

然后你可以这样做: //This is a cache, which holds all EditFields.

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

}

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

答案 3 :(得分:1)

你可以这样试试

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
}