包装AutoCompleteField列表项

时间:2012-01-24 23:14:20

标签: user-interface blackberry java-me wrapping

我的应用程序有AutoCompleteField,可以保存超过100个字符的长文本,如果我使用常规的AutoCompleteField,我就无法读取剩余的数据。

Cant read the rest item description

如何在autocompletefield选项中将文本换行为2行或更多行? 我尝试使用'\ r'+'\ n'和'\ n',它不给新行。设置它的大小,并设置行高不会给我我想要的结果

AutoCompleteField autoCustomer = new AutoCompleteField(custList, style);
autoCustomer.getListField().setSize(20);
autoCustomer.getListField().setRowHeight(100);

1 个答案:

答案 0 :(得分:0)

如果我是你,我会覆盖drawListRow并使用drawText绘制文本,这将让我完全控制行的外观。尝试调整代码以表现得像这样

AutoCompleteField autoCompleteField = new AutoCompleteField(
                filterList, AutoCompleteField.LIST_STATIC) {

            public void drawListRow(ListField listField, Graphics g,
                    int index, int y, int width) {

        BasicFilteredListResult result = (BasicFilteredListResult) (autoCompleteField
                .get(listField, index));
        if (result != null) 
        {
                          //Draw text here
        }
            }

            public void onSelect(Object selection, int type) {
                super.onSelect(selection, type);
                if (selection != null) {

                    BasicFilteredListResult result = (BasicFilteredListResult) this
                            .getSelectedObject();
                    handleResult((String) result._object);

                } else {
                    Dialog.alert(Resource
                            .getString(PLEASE_PICK_A_VALID_NAME));
                    return;
                }
            }
        };

如果要包装文本,可以使用以下方法

// Handy method to wrap text drawn with the specified font into rows with
// the max width
// Found here:
// http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/m-p/499901
public static String[] wrapText(String text, Font f, int maxWidth) {
    Vector result = new Vector();
    if (text == null)
        return new String[] {};

    boolean hasMore = true;

    // The current index of the cursor
    int current = 0;

    // The next line break index
    int lineBreak = -1;

    // The space after line break
    int nextSpace = -1;

    while (hasMore) {
        // Find the line break
        while (true) {
            lineBreak = nextSpace;
            if (lineBreak == text.length() - 1) {
                // We have reached the last line
                hasMore = false;
                break;
            }
            nextSpace = text.indexOf(' ', lineBreak + 1);
            if (nextSpace == -1)
                nextSpace = text.length() - 1;
            int linewidth = f
                    .getAdvance(text, current, nextSpace - current);
            // If too long, break out of the find loop
            if (linewidth > maxWidth)
                break;
        }
        String line = text.substring(current, lineBreak + 1);
        result.addElement(line);
        current = lineBreak + 1;
    }
    String[] resultArray = new String[result.size()];
    result.copyInto(resultArray);
    return resultArray;
}