代码之家  ›  专栏  ›  技术社区  ›  MikeyB

在BlackBerry上创建应用程序信息/帮助屏幕

  •  2
  • MikeyB  · 技术社区  · 15 年前

    我试图为我的应用程序创建一个帮助/关于屏幕,但我发现,嗯,我的代码很糟糕。(我知道它可能需要一点重构,但是当使用一个新框架时,我会先让代码工作,然后立即返回并重构以“正确”地执行操作)。

    首先,我所做的并不“感觉”是正确的做法。我不确定是否只在布局中填充一堆文本字段——有更好的方法吗?

    第二,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);
        }
    }
    
    1 回复  |  直到 13 年前
        1
  •  2
  •   Dan J Akhil    13 年前

    对于“感觉不正确”的代码——在功能上看起来很好,但是一点重构可以将其清理一点——也许可以创建和填充文本字段的方法。

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

    您可以使用自定义字段管理器来解决布局问题——这并不困难,只需对管理器进行子类划分并实现子布局即可。定义一个“top”、“bottom”和“middle”字段,并相应地进行布局(有些代码遗漏了,作为对读者的练习,但基本上在向管理器添加字段时,您需要能够指定一个为top,一个为bottom。以下逻辑将确保底部字段始终固定在底部,并且顶部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());
    }
    

    同样,只是一个框架-没有错误检查或任何内容。然后将您的代理设置为这个新的管理器,将垂直字段管理器设置为中间字段(您可能需要指定的setter),将按钮字段设置为底部字段(同样,指定的getter)。