我想让EditText的最大行数为2,最大长度为20。如果EditText长度大于10,它会自动转到新行,这样用户就不需要按Enter了。有没有人能帮我解决这个问题?
发布于 2016-09-13 19:45:50
将其添加到您的EditText xml代码中
android:inputType="textMultiLine“
这将在输入数据时自动将文本移动到下一行。
发布于 2014-04-17 14:01:00
它应该以一种最优雅的方式存在,但这个解决方案可能会帮助你实现你想要的东西。首先,您需要设置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" /> 您必须将maxLength 21属性设置为 19,因为enter 21(新行)将在编辑文本中使用一个字符,这样用户将只能写入19字符,而不能写入20。
然后,您应该使用带有boolean的TextWatcher (在用户删除之前的字符时使用),这应该如下所示:
// 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;
}
}); 注意:但是,您应该小心使用此代码。实际上,用户仍然可以按下Key Enter,然后添加新行。也许这些答案可能会帮助你处理它,并让用户只在你的道路上:
发布于 2014-04-17 11:26:16
使EditText显示2行。你可以这样写:
android:maxLength="20"
android:maxLines="2"
android:singleLine="false"
android:layout_width="Your Choice"https://stackoverflow.com/questions/23123833
复制相似问题