问题
I am having one horizontal field manager i am adding some components at run time.I want to know which are the components i have placed in the horizontalfieldmanager.Can any one give me idea how to find which are the components are present in horizontal field manager. regards, s.kumaran.
回答1:
+1 Jan, but what about inner Managers? Some recursion will do the thing.
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());
}
}
}
}
回答2:
Something like this should work:
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;
}
来源:https://stackoverflow.com/questions/1357345/blackberry-get-all-child-fields-of-control