Android EditText 自动换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23123833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText automatically go to a new line
提问by Frank Junior
I want to make the EditText
with max lines of 2 and max length of 20. If the EditText
length is more than 10, it should automatically go to a new line so the user don't need to press Enter. Can someone help me with this requirement?
我想让EditText
最大行数为 2,最大长度为 20。如果EditText
长度超过 10,它应该自动换行,这样用户就不需要按 Enter。有人可以帮我满足这个要求吗?
回答by rkrohit
ADD this to your EditText xml code
将此添加到您的 EditText xml 代码中
android:inputType="textMultiLine"
android:inputType="textMultiLine"
This will automatically moves your text to next line while entering data.
这将在输入数据时自动将您的文本移动到下一行。
回答by fllo
It should exist a most elegant way but this solution might help you as a clue to achieve what you want. First of all, you will need to set your EditText
values as below:
它应该以最优雅的方式存在,但此解决方案可能会帮助您作为实现您想要的目标的线索。首先,您需要设置EditText
如下值:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textMultiLine|textCapSentences"
android:maxLength="21"
android:gravity="left"
android:maxLines="2" />
You must to set the maxLength
attribute to 21
because the enter char(new line) will take one char in the edittext, then the user will only can write 19
chars instead of 20
.
Then, you should use a TextWatcher
with a boolean
(used if the user removes his previous chars), and this should be as follows:
您必须将该maxLength
属性设置为,21
因为输入字符(新行)将在编辑文本中占用一个字符,那么用户将只能写入19
字符而不是20
.
然后,您应该使用 aTextWatcher
和 a boolean
(如果用户删除了他以前的字符,则使用),这应该如下所示:
// init global boolean
private boolean isReached = false;
// in onCreate method
edittext.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
// if edittext has 10chars & this is not called yet, add new line
if(textEd.getText().length() == 10 && !isReached) {
textEd.append("\n");
isReached = true;
}
// if edittext has less than 10chars & boolean has changed, reset
if(textEd.getText().length() < 10 && isReached) isReached = false;
}
});
Note:However, you should be careful with this code. Indeed, the user can still pressed the Key Enter
and then, add new lines. Maybe these answers might help you to handle it and keep the user only "on your road": Prevent enter key on EditText but still show the text as multi-line
注意:但是,您应该小心使用此代码。实际上,用户仍然可以按Key Enter
,然后添加新行。也许这些答案可能会帮助您处理它并使用户只“在路上”:防止在 EditText 上输入键但仍将文本显示为多行
回答by prakash
For the EditText to display 2 lines. You can write like this:
让 EditText 显示 2 行。你可以这样写:
android:maxLength="20"
android:maxLines="2"
android:singleLine="false"
android:layout_width="Your Choice"
回答by Luis Lavieri
For the lines, do something like:
对于线条,请执行以下操作:
<EditText
android:inputType="textMultiLine" <!-- Multiline input -->
android:lines="2" <!-- Total Lines prior display -->
android:minLines="1" <!-- Minimum lines -->
android:gravity="top|left" <!-- Cursor Position -->
android:maxLines="2" <!-- Maximum Lines -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
you could also play with this parameter (Does NOT work for Android <= 3.0):
您也可以使用此参数(不适用于 Android <= 3.0):
android:singleLine="false"
And for the number of characters, do something like:
对于字符数,请执行以下操作:
EditText et= (EditText) findViewById(R.id.editText1);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(20); //Filter to 20 characters
et.setFilters(filters);
or, use this other parameter,
或者,使用这个其他参数,
android:maxLength="20"
in the xml
android:maxLength="20"
在里面 xml