2009-06-25 141 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