【问题标题】:Wrap AutoCompleteField List Item包装 AutoCompleteField 列表项
【发布时间】:2012-02-18 05:20:42
【问题描述】:

我的应用有 AutoCompleteField,其中包含超过 100 个字符的长文本,如果我使用常规 AutoCompleteField,我无法读取其余数据。

如何在自动完成字段选项中使文本换行成 2 行或更多行? 我尝试使用 '\r'+'\n' 和 '\n',它没有换行。设置它的大小并设置行高并没有给我想要的结果

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

【问题讨论】:

    标签: user-interface blackberry java-me word-wrap


    【解决方案1】:

    如果我是你,我会覆盖 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;
    }
    

    【讨论】:

    • public void drawListRow(ListField listField, Graphics g, int index, int y, int width) y 指的是什么?,索引似乎只是现在显示的索引,例如它总是显示 1 ,2,3 即使结果大于 3
    • y 是从列表字段顶部开始绘制的距离。 index 是正在显示的行索引
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 2017-08-27
    • 1970-01-01
    • 2022-10-17
    • 2019-12-07
    • 2021-12-16
    相关资源
    最近更新 更多