简体   繁体   English

在Android EditText中按字母换行

[英]Break line by letters in Android EditText

How to break Line by letters in android EditText , so it became like this: 如何通过android EditText的字母EditText ,所以变成了这样:

Hello how to br
eak by letters
instead of word
s

Instead of this: 代替这个:

Hello how to
break by 
letters
instead of 
words

The analog in css is word-break: break-all CSS中的模拟是word-break: break-all

PS Of course Text is dynamically changing. PS当然,文字是动态变化的。

Try this // tmpString - the string to display 试试这个// tmpString-要显示的字符串

//15 - no. // 15-不。 of letters you want change according to your requirement 您想要更改的字母的数量根据您的要求

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);

It calls 'justify text' or 'text justification', but in Android it is not supported, at least until now. 它称为“对齐文本”或“文本对齐”,但至少在现在为止,在Android中不支持。

There a kind of solution if you want to implement. 如果要实施,有一种解决方案。 It is to use WebView and load the text in HTML format and set the body style to justify . 它是使用WebView并以HTML格式加载文本并将body样式设置为justify It is just a solution if you really need 'text justification'. 如果您确实需要“文本对齐”,这只是一个解决方案。

You can use auto resize edit text with following properties: 您可以使用具有以下属性的自动调整大小的编辑文本:

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

Full EditText layout example: 完整的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"/>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM