2011-05-26 31 views
4

缺省狀態下,一個Android的EditText會斷一次線,如果線長於視圖,這樣後:如何防止的EditText從打破了線標點符號

Thisisalineanditisveryverylongs (end of view) 
othisisanotherline 

,或者如果行包含一個標點符號,像這樣:

Thisisalineanditsnotsolong;  (several characters from the end of view) 
butthisisanotherline 

由於我的工作要求,文本必須斷行只有在訂單長於來看,是這樣的:

Thisisalineanditsnotsolong;andt (end of view) 
hisisanotherline 

必須有一種方法來實現這一點,我說得對嗎?到目前爲止,我還沒有找到這樣做。

回答

4

TextView(和EditText)破壞文本的方式是通過對BoringLayout的內部私有函數調用。所以,最好的方法是將EditText重寫並重寫這些函數。但這不會是一件微不足道的任務。

因此,在TextView class有文字風格的不同類的創作。我們看的是DynamicLayout。在本課中,我們可以獲得StaticLayout(在一個名爲reflowed的變量中)的參考。在這個類的構造函數中,您將找到文本換行算法:

/* 
* From the Unicode Line Breaking Algorithm: 
* (at least approximately) 
* 
* .,:; are class IS: breakpoints 
*  except when adjacent to digits 
*/ is class SY: a breakpoint 
*  except when followed by a digit. 
* - is class HY: a breakpoint 
*  except when followed by a digit. 
* 
* Ideographs are class ID: breakpoints when adjacent, 
* except for NS (non-starters), which can be broken 
* after but not before. 
*/ 

if (c == ' ' || c == '\t' || 
((c == '.' || c == ',' || c == ':' || c == ';') && 
(j - 1 < here || !Character.isDigit(chs[j - 1 - start])) && 
(j + 1 >= next || !Character.isDigit(chs[j + 1 - start]))) || 
((c == '/' || c == '-') && 
(j + 1 >= next || !Character.isDigit(chs[j + 1 - start]))) || 
(c >= FIRST_CJK && isIdeographic(c, true) && 
j + 1 < next && isIdeographic(chs[j + 1 - start], false))) { 
okwidth = w; 
ok = j + 1; 

這裏是所有包裝的內容。所以你需要繼承StaticLayout,DynamicLayout,TextView和最後EditText的子類,我相信這將是一場噩夢:(我甚至不知道所有的流程如何。如果你想要的話 - 首先看看TextView並檢查getLinesCount調用 - 這將是起點

+0

非常有趣...因爲我已經在我的工作中使用了EditText。你能指出我正確的方向,我應該重寫哪些方法來完成這項工作? – hoangbv15 2011-05-26 10:28:11

+0

我不確定是否有任何其他方式,無論如何,這是一個巨大的解決方案,這樣一個小問題。如果找不到其他解決方案,我會研究這一點。非常感謝你。 – hoangbv15 2011-05-26 14:40:48

+0

我遇到了類似的問題,並提出了以下解決方案:https://groups.google.com/forum/?fromgroups=#!topic/android-discuss/JDAXbJ5IDcE我希望他們只是公開那段代碼,而不是隱藏它。 – 2013-01-30 18:18:36

3

Android中的這種換行算法真的很糟糕,它甚至在邏輯上不正確 - 逗號不能是行的最後一個字符,它只會產生不必要的換行,這會導致非常奇怪的文字排版。

1

你好這裏是一個方法,我第一次從另一個人得到,然後作出一點點的改變,這真的對我的作品,你可以試試。

//half ASCII transfer to full ASCII 
public static String ToSBC(String input) { 
    char[] c = input.toCharArray(); 
    for (int i = 0; i< c.length; i++) { 
    if (c[i] == 32) { 
    c[i] = (char) 12288; 
    continue; 
    } 
    if (c[i]<=47 && c[i]>32) 
    c[i] = (char) (c[i] + 65248); 
    } 
    return new String(c); 
    } 
} 

它在這裏。我將一些特殊字符從半角改成了全角,如「,」,「。」,效果相當不錯。你可以試試。