在一行中限制文本长度EditText Android [英] Limit text length in a line EditText Android

查看:97
本文介绍了在一行中限制文本长度EditText Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中使用多行EditText限制一行中的字符数 我这样做是因为不允许多行

How to limit number of character in a line with multi-line EditText in android I am doing like this by this is not allowing multiline

  <EditText
        android:id="@+id/MsgText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_margin="5sp"
        android:layout_weight="1"
        android:background="@drawable/message_field"
        android:inputType="textMultiLine"
        android:maxLength="10"
        android:padding="5sp"
        android:hint="Type a Message"
        android:textColor="@color/textFieldColor" />

推荐答案

您可以使用它.

final EditText edit = (EditText)findViewById(R.id.MsgText);
final int maxLineLength = 10;

edit.addTextChangedListener(new TextWatcher() {
    final Integer mark = 1;
    String textBeforeEdit = null;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        textBeforeEdit = s.toString().substring(start, start + count);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        edit.getText().setSpan(mark, start, start + count, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    @Override
    public void afterTextChanged(Editable s) {
        String str = s.toString();
        int spanStart = s.getSpanStart(mark);
        int spanEnd = s.getSpanEnd(mark);
        int lastNL = str.lastIndexOf('\n', spanStart);
        int nextNL;
        while(lastNL < spanEnd) {
            nextNL = str.indexOf('\n', lastNL + 1);
            if(nextNL == -1)
                nextNL = str.length();
            if(nextNL - lastNL > maxLineLength + 1) {
                // reject the entire change
                s.replace(spanStart, spanEnd, textBeforeEdit);
                break;
            }
            lastNL = nextNL;
        }
        s.removeSpan(mark);
    }
});

代码的作用是监视文本,每次更改文本(可能同样是键盘输入或粘贴)时,是否有任何受影响的行增长到超过maxLineLength个字符(为\n加一个) ).我们不在乎更改之前或之后的行,因此我们可以从重写的区域开始之前的最后一个\n开始计数. (如果lastIndexOf给出的-1也很好.)我们找到下一个\n,如果最后一个字符后不超过maxLineLength + 1个字符,那很好,我们前进到跨度之后(或在字符串的结尾).

What the code does is that it watches, at each change to the text (which may equally be keyboard input or paste), whether any affected line has grown to more than maxLineLength characters (plus one for the \n). We don't care about lines before or after the change, so we can start counting at the last \n immediately preceding the start of the region that was rewritten. (If the lastIndexOf gives -1 that's fine, too.) We find the next \n, if it's no more than maxLineLength + 1 characters past the last, that's fine and we advance until after the span (or at the end of the string).

当条件被破坏时,事情会变得有趣:因此,我们在已更改区域的开头和结尾(相对于 new 的开头)存储了一个不可见的标记.文字)以及被重写的原始文字.通过用旧内容替换新区域,我们有效地拒绝了更改,因此该动作将被忽略.

Things get interesting when the condition is ever broken: for this reason we store an invisible mark at the beginning and at the end of the region that has been changed (relative to the start of the new text) as well as the original text that was rewritten. By replacing the new region by the old contents we effectively reject the change, so the action will be ignored.

这种方法的好处是

  • 它在插入时也能很好地与粘贴一起使用,
  • 如果结果太长,将不允许您合并行(使用退格键)
  • 它永远不会更改您不希望更改的任何文本(例如简单地将行缩短或中断为10个字符),
  • 它提供了预期的用户行为,而没有闪烁.您按下了不合适的键-什么也没发生.

这篇关于在一行中限制文本长度EditText Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆