在Android EditText中按字母分隔

时间:2016-03-03 05:26:56

标签: java android android-edittext

如何在android EditText中逐字逐句打破,所以它变成了这样:

Hello how to br
eak by letters
instead of word
s

而不是:

Hello how to
break by 
letters
instead of 
words

css中的模拟是word-break: break-all

P.S。当然Text正在动态变化。

3 个答案:

答案 0 :(得分:0)

试试这个 // tmpString - 要显示的字符串

// 15 - 没有。你想要的字母根据你的要求改变

int index = 0;
StringBuffer finalString = new StringBuffer(); 
while (index < tmpString.length()) {
    finalString.append(tmpString.substring(index, Math.min(index + 15,tmpString.length()))+"\n");
    index += 15;
}
editText.setText(finalString);

答案 1 :(得分:0)

它呼吁“证明文本”的合理性。或者&#39;文本理由&#39;,但在Android中,它不受支持,至少到现在为止。

如果你想实施,有一种解决方案。要使用WebView并以HTML格式加载文本,并将body样式设置为justify。如果你真的需要文本证明,那只是一个解决方案。

答案 2 :(得分:0)

您可以使用具有以下属性的自动调整大小编辑文本:

android:inputType="textMultiLine"
android:layout_height="wrap_content"

完整的EditText布局示例:

<EditText
   android:layout_width="match_parent"
   android:inputType="textMultiLine"
   android:layout_height="wrap_content"
   android:hint="Enter Text"
   android:textColorHint="@color/primary_color"
   android:ems="10"
   android:imeOptions="actionDone"
   android:id="@+id/message_input"
   android:layout_gravity="center_horizontal"/>