黑莓 - 获得所有儿童控制领域

时间:2009-08-31 12:58:28

标签: user-interface blackberry

我有一个水平字段管理器我在运行时添加一些组件。我想知道我在horizo​​ntalfieldmanager中放置了哪些组件。任何人都可以告诉我如何找到组件中存在的组件水平现场经理。 问候, s.kumaran。

2 个答案:

答案 0 :(得分:4)

+1 Jan,但内部经理呢?一些递归会做的事情。

class Scr extends MainScreen implements FieldChangeListener {
    HorizontalFieldManager mMainPanel;
    VerticalFieldManager mVerticalPanel;
    ButtonField mPrintButton;

    public Scr() {
        add(new LabelField("Title", FIELD_HCENTER));
        mMainPanel = new HorizontalFieldManager();
        add(mMainPanel);
        mVerticalPanel = new VerticalFieldManager();
        mMainPanel.add(mVerticalPanel);
        for (int i = 0; i < 5; i++) {
            HorizontalFieldManager hfm = new HorizontalFieldManager();
            hfm.add(new LabelField("Label " + i, FIELD_LEFT));
            hfm.add(new BasicEditField(FIELD_RIGHT));
            mVerticalPanel.add(hfm);
        }
        mPrintButton = new ButtonField("Print Console All Fields",
                ButtonField.CONSUME_CLICK | FIELD_HCENTER);
        mPrintButton.setChangeListener(this);
        add(mPrintButton);
    }

    private Field[] getAllFields(Manager manager) {
        int cnt = manager.getFieldCount();
        Field[] result = new Field[cnt];

        for (int i = 0; i < cnt; i++) {
            Field field = manager.getField(i);
            result[i] = field;

            if (field instanceof Manager) {
                Field[] childFields = getAllFields((Manager) field);
                result = appendArray(result, childFields);
            }
        }
        return result;
    }

    private Field[] appendArray(Field[] target, Field[] source) {
        for (int i = 0; i < source.length; i++) {
            Arrays.add(target, source[i]);
        }
        return target;
    }

    public void fieldChanged(Field field, int context) {
        if (mPrintButton == field) {
            Field[] fields = getAllFields(getMainManager());
            for (int i = 0; i < fields.length; i++) {
                System.out.print("\nN " + i + "; Class: "
                        + fields[i].getClass().getName());
            }
        }
    }
}

答案 1 :(得分:1)

这样的事情应该有效:

public Field[] getFieldsOfManager(Manager manager) {
    Field[] fields = new Field[manager.getFieldCount()];
    for( int i = 0; i < manager.getFieldCount(); i++ )
    {
        fields[i] = manager.getField( i );        
    }
    return fields;
}