在Blackberry上创建应用程序信息/帮助屏幕
我试图为我的应用程序创建一个帮助/关于屏幕,但我发现,我的代码很糟糕。(我知道它可能需要一些重构,但是当使用一个新框架时,我先让代码正常工作,然后立即返回并重构以“正确”地完成工作) 首先,我所做的“感觉”不是正确的方式。我不确定是否只是在布局中填充一堆文本字段——有没有更好的方法 其次,VFM占据了屏幕的大部分,并按下了底部的“关闭”按钮。我试图做的是保持标题和“关闭”按钮可见,但只是滚动VFM 如何解决这些问题在Blackberry上创建应用程序信息/帮助屏幕,blackberry,Blackberry,我试图为我的应用程序创建一个帮助/关于屏幕,但我发现,我的代码很糟糕。(我知道它可能需要一些重构,但是当使用一个新框架时,我先让代码正常工作,然后立即返回并重构以“正确”地完成工作) 首先,我所做的“感觉”不是正确的方式。我不确定是否只是在布局中填充一堆文本字段——有没有更好的方法 其次,VFM占据了屏幕的大部分,并按下了底部的“关闭”按钮。我试图做的是保持标题和“关闭”按钮可见,但只是滚动VFM 如何解决这些问题 public class HelpScreen extends PopupScr
public class HelpScreen extends PopupScreen {
public HelpScreen() {
super(new VerticalFieldManager(), Field.FOCUSABLE);
/* Construct the Close button */
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ok();
}
};
ButtonField b = new ButtonField("Close", Field.FIELD_HCENTER);
b.setChangeListener(listener);
/* Construct the text box containing the help */
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL);
TextField f;
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("My application does stuff. This part is the description of what it does.");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("Commands:");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("N - New Widget");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("R - Rename Widget");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("D - Duplicate Widget");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("C - Clear Widget");
vfm.add(f = new TextField(FIELD_LEFT | READONLY));
f.setText("Shift-Delete - Delete Widget");
/* Construct the screen */
add(new LabelField("About Widget Wiffleball", Field.FIELD_HCENTER));
add(new SeparatorField());
add(vfm);
add(b);
}
public void ok() {
UiApplication.getUiApplication().popScreen(this);
}
}
对于感觉不“正确”的代码——从功能上看,这似乎很好,但一点重构可以让它变得更干净——也许可以创建一个方法来创建和填充文本字段
private TextField createTextField(String content) {
TextField textField = new TextField(
}
然后,构造函数的该部分变成:
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL);
vfm.add(createTextField("My application does stuff. This part is the description of what it does."));
vfm.add(createTextField("Commands:"));
vfm.add(createTextField("N - New Widget"));
vfm.add(createTextField("R - Rename Widget"));
vfm.add(createTextField("D - Duplicate Widget"));
vfm.add(createTextField("C - Clear Widget"));
vfm.add(createTextField("Shift-Delete - Delete Widget"));
您可以使用自定义字段管理器来解决布局问题—其实并不困难,您只需将管理器子类化并实现子布局即可。定义“顶部”、“底部”和“中间”字段,并相应地进行布局(一些代码作为练习留给读者,但基本上在向管理器添加字段时,您需要能够指定一个为顶部,一个为底部。以下逻辑将确保底部字段始终固定在底部,并且顶部的两个字段不会将其推离底部:
protected void sublayout(int width, int height) {
// retrieve the top, middle and bottom fields
int heightRemaining = height;
layoutChild(topField, width, heightRemaining);
setPositionChild(topField, 0, 0);
heightRemaining -= topField.getHeight();
layoutChild(bottomField, width, heightRemaining);
setPositionChild(bottomField, 0, height - bottomField.getHeight());
heightRemaining -= bottomField.getHeight();
layoutChild(middleField, width, heightRemaining);
setPositionChild(middleField, 0, topField.getHeight());
}
同样,只是一个框架-没有错误检查或任何内容。然后将您的委托设置为这个新的管理者,将您的垂直字段管理者设置为中间字段(您可能需要指定的setter),将您的按钮字段设置为底部字段(同样,指定的getter).谢谢-我会试试。我知道一些重构会有帮助;当使用新框架时,我倾向于“让它工作”,然后回去立即重构。“感觉不对劲”这是因为使用一堆文本字段似乎有点不妥。另一方面,也许这是正确的方法。使用主屏幕的状态部分也是处理这个问题的好方法。这样,您就不需要自定义管理器了。