2012-06-01 30 views
1

我有一個AutoCompleteField在我的黑莓應用程序的屏幕之一。我必須顯示一個佔位符文本以提供用戶輸入信息的提示。在黑莓中的自動完成字段上放置文本文本

這裏是AutoCompleteField

BasicFilteredList filterList = new BasicFilteredList(); 
     String[] address = { "T 115 Centro Galleria Shopping Centre, Cnr Old Collier and Walters Road Morley WA 1522", 
       "1423 SEAVIEW POINT POINT COOK VIC 2674", 
       "Lot 1498 Yarraman Road Wyndham Vale VIC 3795", 
       "Lot 3506 Witchmount Close Hillside VIC 4055", 
       "6 Paas Place Williamstown VIC 4233", 
       "Lot 99 14 James Close Sunbury VIC 4502", 
       "1 Charlotte Street Clayton South VIC 4779" }; 

     filterList.addDataSet(1, address, "address", BasicFilteredList.COMPARISON_IGNORE_CASE); 
     AutoCompleteField autoCompleteField = new AutoCompleteField(filterList){ 
      public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) { 
       ListField _list = getListField(); 
       if (_list.getSelectedIndex() > -1) { 
        if(selectedText!=null){ 
         BasicFilteredListResult result = (BasicFilteredListResult) selection; 
         selectedText.setText(result._object.toString()); 
        } 
       } 
      } 
     }; 
     add(autoCompleteField); 

任何的下面的代碼,請建議我如何能實現相同。

謝謝。

回答

2

您可以使用與the one shown here for normal EditFields類似的技術。基本上,您需要覆蓋AutoCompleteField子類中的paint()方法。在paint()中,檢查該字段是否爲空,如果是,則手動繪製所需的佔位符文本。

區別在於AutoCompleteFieldManager,其內部爲BasicEditField。因此,要正確繪製文本,需要計算父ManagerAutoCompleteField)內編輯字段的x和y偏移量。

所以,這個類的一個實例來替換你的AutoCompleteField例如:

private class CustomAutoCompleteField extends AutoCompleteField { 
     private int yOffset = 0; 
     private int xOffset = 0; 

     public CustomAutoCompleteField(BasicFilteredList filteredList) { 
     super(filteredList); 
     } 

     protected void paint(Graphics g) { 
     super.paint(g); 
     if (xOffset == 0) { 
      // initialize text offsets once 
      xOffset = getEditField().getContentLeft(); 
      yOffset = getEditField().getContentTop(); 
     } 
     String text = getEditField().getText(); 
     if (text == null || text.length() == 0) { 
      int oldColor = g.getColor(); 
      g.setColor(Color.GRAY); 
      g.drawText("enter text", xOffset, yOffset); 
      g.setColor(oldColor); 
     } 
     } 

     public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) { 
     ListField _list = getListField(); 
     if (_list.getSelectedIndex() > -1) { 
      if(selectedText!=null){ 
       BasicFilteredListResult result = (BasicFilteredListResult) selection; 
       selectedText.setText(result._object.toString()); 
      } 
     } 
     } 
    } 

我測試了這個在OS 5.0,與沒有任何保證金或填充集的實例。有可能在不同的佈局下,您可能需要調整計算x和y偏移量的邏輯。但是,上面的代碼顯示了您的基本想法。祝你好運。

編輯:上述代碼的提示與警告,您的onSelect()方法顯然是依賴於未顯示的代碼。就像上面的代碼不會被編譯。我在那裏只留下onSelect()只是爲了表明我基本上只是替換原來的匿名類,並且在onSelect()方法中沒有做任何不同的事情,因爲它與佔位符文本問題沒有直接關係。

+0

謝謝Nate。我會試試看。 –