删除字符和字符长度限制在MultiLine EditText Android中不起作用

时间:2018-08-14 05:52:59

标签: android android-edittext multiline android-textwatcher

我正在使用多行编辑文本,我在Text Watcher中限制字符不能超过42个,但是我无法删除其编辑文本中的char字符,因为它移到了新行中。线超出限制?

XML代码:

 <EditText
            android:id="@+id/edt_footer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Thank you visit again"
            android:maxLines="3"
            android:lines="3"
            android:ems="18"
            android:scrollbars="vertical"
            android:inputType="textMultiLine"
            android:gravity="left|top"
            android:layout_marginLeft="@dimen/margin_20"
            android:textSize="20sp" /> 

Java代码:

footerEdt.addTextChangedListener(new TextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (count % 42 == 0)
                {
                    ((Editable) s).append("\n");
                    footerEdt.setSelection(footerEdt.getText().length());
                }

            }
        });

1 个答案:

答案 0 :(得分:0)

尝试一下

footerEdt.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (count % 42 == 0)
            {
                ((Editable) s).append("\n");
                footerEdt.setText(s);
                footerEdt.setSelection(footerEdt.getText().length());
            }

        }
    });