在editfield中设置选定的值?

时间:2014-09-10 11:38:27

标签: blackberry

我制作了自定义自动推荐列表。

我想在编辑字段中设置选择的值。 我正在使用此Place holder text on a AutoCompleteField in blackberry

CustomAutoCompleteField.js     打包mypackage;

import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;

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) {
           final String selectedText = getEditField().getText();
         if(selectedText!=null){
             final BasicFilteredListResult result = (BasicFilteredListResult) selection;
             Application.getApplication().invokeLater(new Runnable() {
                  public void run() {
                     selectedText.setText(result._object.toString()); 
                  }
                });
            // selectedText.setText(result._object.toString());
          }
       }
    }
    protected void sublayout(int maxWidth, int maxHeight) {
        // TODO Auto-generated method stub
        super.sublayout(250, 250);
    }

 }

myscreen.js

public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        // Set the displayed title of the screen     

        BasicFilteredList filterList = new BasicFilteredList();
        String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
                        "Thursday","Friday","Saturday","Sunday(I_)"};
        filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);

        HorizontalFieldManager hr =new HorizontalFieldManager();
        LabelField userName= new LabelField("hjsdhas");
        hr.add(userName);
        CustomAutoCompleteField autoCompleteField = new CustomAutoCompleteField(filterList);

        hr.add(autoCompleteField);
        add(hr);

    }
}

CustomAutoCompleteField.java是我的自定义java类,我从myscreen.java类调用该类

0 个答案:

没有答案