2009-06-25 85 views
2

我正在尝试为我的应用程序创建一个帮助/关于屏幕,但我发现,我的代码太糟糕了。 (我知道它可以使用一些重构,但是当使用新框架时,我会先让代码工作,然后立即返回并重构“正确地”执行任务)。在黑莓上创建应用程序信息/帮助屏幕

首先,我在做什么并没有“感觉”像做的正确的方式。我不确定只是将一堆文本字段填充到布局中 - 有没有更好的方法来实现?

其次,VFM是占用了大量屏幕,并推着我的“关闭”按钮关闭底部。我试图做的是保持标题和'关闭'按钮可见,但只需滚动VFM。

我该如何解决这些问题?

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

回答

2

的代码不是“感觉正确” - 在功能上似乎不错,但有点重构的可能它清理干净一点 - 也许是为了营造一种方法和填充的TextField

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")); 

您可以使用自定义字段管理解决布局问题 - 真的不难,你只需要继承经理和执行苏blayout。定义一个'顶部','底部'和'中间'字段,并相应地进行布局(一些代码作为练习给读者留下了,但基本上当向管理器添加字段时,您需要能够指定一个作为顶部和一个底部作为下面的逻辑将确保底场总是粘在底部,顶部2场不推断底:

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

再次,只是一个框架 - 无错误检查或其中的任何内容,然后将您的委托设置为这个新的管理器,将您的垂直领域管理器设置为中间字段(您可能需要指定的设置器),将按钮字段设置为底部字段(再次指定getter )。

+0

谢谢 - 我会试试看,我知道一些重构克会有所帮助;当使用新的框架时,我倾向于“让它工作”,然后返回并立即重构。 “不正确的感觉”来自于这样一个事实,即使用一堆文本字段似乎有点令人费解。另一方面,或许那*是正确的方式。 – MikeyB 2009-06-25 03:19:42