Place holder text on a AutoCompleteField in blackberry

后端 未结 1 1694
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 19:42

I have tp place a AutoCompleteField in one of my screen in Blackberry app. I have to show a place holder text to provide hint for user to enter the information.

相关标签:
1条回答
  • 2021-01-23 19:57

    You can use a similar technique to the one shown here for normal EditFields. Basically, you need to override the paint() method in an AutoCompleteField subclass. In paint(), you check and see if the field is empty, and if so, you manually draw the placeholder text you want.

    The difference is that AutoCompleteField is a Manager with a BasicEditField inside of it. So, to draw the text properly, you need to figure out the x and y offsets of the edit field within the parent Manager (the AutoCompleteField).

    So, replace your AutoCompleteField instance with an instance of this class:

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

    I tested this on OS 5.0, with an instance that didn't have any margin or padding set. It's possible that with different layouts, you may need to adjust the logic for calculating the x and y offsets. But, the above code shows you the basic idea. Good luck.

    Edit: the above code is presented with the caveat that your onSelect() method is clearly relying on code not shown. As is, the above code won't compile. I left onSelect() in there just to show that I'm essentially just replacing the anonymous class you originally had, and not doing anything different in your onSelect() method, as it's not directly related to the placeholder text issue.

    0 讨论(0)
提交回复
热议问题