简体   繁体   English

具有文本限制的多行编辑文本

[英]Multiline edittext with text limit

How can I create a multiline edittext with text limit?如何创建具有文本限制的多行编辑文本? I set the maxLength of edittext but it made the edittext singleline and if I add inputType to textMultiLine then the imeOptions set as actionDone won't show.我设置了 edittext 的 maxLength 但它使 edittext 单行,如果我将 inputType 添加到 textMultiLine 那么设置为 actionDone 的 imeOptions 将不会显示。 I don't want to set static value for the maxLines attribute.我不想为 maxLines 属性设置静态值。

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/activityNameHint"
        android:inputType="textMultiLine"
        android:maxLength="500"
        android:imeOptions="actionDone"
       />

And also when the edittext reaches its limit the keyboard is still allowing the text which makes it difficult to delete the text.而且当 edittext 达到其限制时,键盘仍然允许文本,这使得删除文本变得困难。 How to solve this issue?如何解决这个问题?

new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {

}

}); });

by overriding text change listener, you can specify the max length in ontextchange() and add condition there and do what ever you want通过覆盖文本更改侦听器,您可以在 ontextchange() 中指定最大长度并在那里添加条件并执行您想要的操作

You can try this code:你可以试试这个代码:

android:maxLines="3"
android:minLines="3"
android:gravity="left"
android:maxLength="500"
android:inputType="textMultiLine"

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

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