天天看點

J2ME實作展示内容自動根據螢幕寬度換行的功能

  由于lwuit的label中沒有根據内容自動換行的設定,而項目中的TextArea的樣式和Label樣式的背景不同,不能使用TextArea展示内容,是以就重寫了一個TextLabel的類,繼承自TextArea類。實作了這個功能,其中大部分源碼參考了http://j2me.cc43.com/100916/124.html中的代碼。

import java.util.Vector;

import com.sun.lwuit.Display;

import com.sun.lwuit.Font;

import com.sun.lwuit.TextArea;

import com.sun.lwuit.geom.Dimension;

import com.sun.lwuit.plaf.Style;

public class TextLabel extends TextArea

{

    private int autoWidth = 0;// 設定換行的寬度

    private int maxRow = 0;// 最大行數

    private boolean maxrowCode = false;// 是否設定了最大行數

    private int fontCountW = 0;// 通過漢字個數計算的寬度

    private final int fontFx = 14;// 字型的size,現用14個像索

    private Vector rowStrings;

    private int widthForRowCalculations = -1;

    public TextLabel()

    {

        this("", Display.getInstance().getDisplayWidth());

    }

    public TextLabel(String text)

    {

        this(text, Display.getInstance().getDisplayWidth());

    }

    public TextLabel(String text, int width)

    {

        super(text);

        System.out.println("螢幕寬度:" + Display.getInstance().getDisplayWidth());

        System.out.println("實際寬度:" + width);

        this.setEditable(false);

        // 不能編輯

        this.setFocusable(false);

        // 不能獲得焦點

         this.getStyle().setBorder(null);

        // 沒有邊框

        this.autoWidth = width;

        this.setScrollSize(new Dimension(0, 0));

        // 設定無滾動條

        this.getStyle().setPadding(0, 0, 4, 15);

        this.setPreferredW(this.autoWidth);

    }

    public TextLabel(String text, int width, int maxRow)

    {

        super(text);

        this.setEditable(false);

        // 不能編輯

        this.setFocusable(false);

        // 不能獲得焦點

        this.maxRow = maxRow;

        if (this.maxRow > 0)

            this.maxrowCode = true;

        this.getStyle().setBorder(null);

        // 沒有邊框

        this.setScrollSize(new Dimension(0, 0));

        // 設定無滾動條

        this.getStyle().setPadding(0, 0, 4, 15);

        this.autoWidth = width;

        this.setPreferredW(this.autoWidth);

    }

    public int getAutoWidth()

    {

        return autoWidth;

    }

    private Vector getRowStrings()

    {

        if (rowStrings == null

                || widthForRowCalculations != getWidth()

                        - getStyle().getPadding(false, RIGHT)

                        - getStyle().getPadding(false, LEFT))

        {

            this.initRowString();

            setShouldCalcPreferredSize(true);

        }

        return rowStrings;

    }

    public int getLines()

    {

        int retVal;

        Vector v = this.getRowStrings();

        retVal = v.size();

        return retVal;

    }

    protected void initRowString()

    {

        rowStrings = new Vector();

        widthForRowCalculations = getWidth();

        if (isSingleLineTextArea())

        {

            rowStrings.addElement(getText());

            return;

        }

        if (getText() == null || getText().equals(""))

        {

            return;

        }

        char[] text = preprocess(getText());

        // 轉化成字元數組

        Style style = this.getStyle();

        int textLabelWidth = this.autoWidth;

        // 本身的寬度

        if (textLabelWidth <= 0)

        {

            textLabelWidth = Display.getInstance().getDisplayWidth();

            // 預設螢幕寬度

        }

        rowStrings = this.autoText(style.getFont(), getText(), textLabelWidth);

    }

    private Vector autoText(Font font, String text, int maxWidth)

    {

        Vector v = new Vector();

        int fcwCode = 0;

        // 用于用漢字寬度計算時

        int piontW = font.charWidth('.') * 3;

        // 計算一個點的寬度

        boolean lastCode = true;

        // 全部顯示出來時為true

        int maxLength = maxWidth ;

        int textLength = font.stringWidth(text);

        if (textLength <= maxLength)

        {// 如果一行容得下時

            v.addElement(text);

            return v;

        }

        else

        {

            // this.maxRow=textLength/maxLength+2;

            System.out.println("maxRow=" + maxRow);

            char[] chars = text.toCharArray();

            // 轉化成字元數組

            int charWidth = 0;

            // 字元寬度

            int rowLength = 0;

            // 每行的長度

            int rowIndex = 0;

            // 每行開始的index

            boolean changeRow = false;

            for (int i = 0; i < chars.length; i++)

            {

                if (chars[i] == '/n')

                {

                    if (rowLength > 0)

                    {

                        v.addElement(text.substring(rowIndex, i));

                        rowLength = 0;

                    }

                    else

                        v.addElement("");

                    i++;

                    rowIndex = i;

                    continue;

                }

                charWidth = font.charWidth(chars[i]);

                if (!changeRow)

                    fcwCode += charWidth;

                else

                    changeRow = false;

                rowLength += charWidth;

                if (this.fontCountW > 0 && fcwCode > this.fontCountW)

                {

                    int j = font.charWidth('.');

                    int w = rowLength - charWidth;

                    if (w > maxLength || (w + j) > maxLength

                            || (w + 2 * j) > maxLength

                            || (w + 3 * j) > maxLength)

                    {

                        v.addElement(text.substring(rowIndex, i));

                        // v.addElement(new String(chars,rowIndex,i-rowIndex));

                        v.addElement("...");

                    }

                    else

                    {

                        v.addElement(text.substring(rowIndex, i) + "...");

                        // v.addElement(new

                        // String(chars,rowIndex,i-rowIndex)+"...");

                    }

                    break;

                }

                if (rowLength > maxLength)

                {

                    changeRow = true;

                    if (chars[i] >= 'A' && chars[i] <= 'z')

                    {

                        // 如果下行第一個為字母時

                        if (i > 0 && chars[i - 1] >= 'A' && chars[i - 1] <= 'z')

                        {

                            int enIndex = 0;

                            // 單詞開始的index

                            for (int j = i; j > 0 && chars[j - 1] >= 'A'

                                    && chars[j - 1] <= 'z'; j--)

                                enIndex = j - 1;

                            if (enIndex > rowIndex)

                            {

                                v.addElement(text.substring(rowIndex, enIndex));

                                // v.addElement(new

                                // String(chars,rowIndex,enIndex-rowIndex));

                                // 把不是字母的放在一行中

                                rowIndex = enIndex;

                                // 下一行的開始位置

                                i = enIndex--;

                                rowLength = 0;

                            }

                            else

                            {

                                v.addElement(text.substring(rowIndex, i));

                                // v.addElement(new

                                // String(chars,rowIndex,i-rowIndex));

                                // 每行的文本

                                rowIndex = i;

                                // 下一行的開始位置

                                i--;

                                // 起點從下一行第一個開始

                                rowLength = 0;

                            }

                        }

                    }

                    else

                    {

                        v.addElement(text.substring(rowIndex, i));

                        // v.addElement(new String(chars,rowIndex,i-rowIndex));

                        // 每行的文本

                        rowIndex = i;

                        // 下一行的開始位置

                        i--;

                        // 起點從下一行第一個開始

                        rowLength = 0;

                    }

                }

                if (i < chars.length - 1)

                    lastCode = false;

                if (this.maxrowCode && v.size() == this.maxRow)

                    break;

            }

            if (this.fontCountW <= 0)

            {

                if (rowIndex <= chars.length - 1)

                {

                    // 最後一行不夠文本時

                    boolean h = false;

                    if (maxRow > 0 && v.size() == this.maxRow)

                    {

                        String vLast = (String) v.lastElement();

                        if (!lastCode)

                        {

                            char[] chs = vLast.toCharArray();

                            int chsLength = 0;

                            if (null != chs)

                                chsLength = chs.length;

                            if (chsLength > 0)

                            {

                                if (font.charWidth(chs[chsLength - 1]) > piontW)

                                    // vLast=(new

                                    // String(chs,0,chsLength-1))+"...";

                                    // 如果最後一個字元的寬度大于三個點的寬度時

                                    vLast = vLast.substring(0, chsLength - 1);

                                else

                                {

                                    if (chsLength > 1

                                            && font

                                                    .charWidth(chs[chsLength - 1])

                                                    + font

                                                            .charWidth(chs[chsLength - 2]) > piontW)

                                        // vLast=(new

                                        // String(chs,0,chsLength-2))+"...";

                                        // 如果最後兩個字元的寬度都大于三個點的寬度時

                                        vLast = vLast.substring(0,

                                                chsLength - 2)

                                                + "...";

                                    else

                                        // vLast=(new

                                        // String(chs,0,chsLength-3))+"...";

                                        // 其它的按三個字元來算

                                        vLast = vLast.substring(0,

                                                chsLength - 3)

                                                + "...";

                                }

                                v.removeElementAt(v.size() - 1);

                                v.addElement(vLast);

                            }

                        }

                    }

                    else

                    {

                        // v.addElement(new

                        // String(chars,rowIndex,chars.length-rowIndex));

                        String vLast = text.substring(rowIndex, chars.length);

                        char[] chs = vLast.toCharArray();

                        int r = 0;

                        for (int i = 0; i < chs.length; i++)

                        {

                            if (chs[i] == '/n')

                            {

                                h = true;

                                v.addElement(vLast.substring(r, i));

                                i++;

                                r = i;

                            }

                        }

                        if (h)

                        {

                            v.addElement(vLast.substring(r, chs.length));

                        }

                        else

                            v.addElement(vLast);

                    }

                }

            }

        }

        System.out.println("list size=" + v.size());

        System.out.println(v.elementAt(0));

        System.out.println(v.elementAt(5));

        return v;

    }

    public String getTextAt(int line)

    {

        Vector rowsV = this.getRowStrings();

        return (String) rowsV.elementAt(line);

    }

    public int getMaxRow()

    {

        return maxRow;

    }

    public void setMaxRow(int maxRow)

    {

        this.maxRow = maxRow;

        this.initRowString();

    }

    public int getFontCountW()

    {

        return fontCountW;

    }

    public void setFontCountW(int fontCount)

    {

        this.fontCountW = fontCount * this.fontFx;

        this.initRowString();

        this.setPreferredSize(new Dimension(this.autoWidth, calcPreferredSize()

                .getHeight()));

    }

}

繼續閱讀