Android: Setting EditText Max Chars per Line?

2019-02-20 00:32发布

Is there any way to set a maximum of Chars to a line in an EditText Field? I tried it with a TextWatcher, But I cant really do what I want.

I would like to set a maximum of lines and a maximum of chars per line.

Example:

Inserted Text:

Lorem ipsum dolor sit amet consetetur sadipscing elitr.

Result (Max-Lines: 10, Max-Chars per line: 20):

Lorem ipsum dolor
sit amet consetetur
sadipscing elitr.

EDIT XML:

    <EditText
    android:id="@+id/edittext"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:background="@drawable/bgedit"
    android:gravity="top"
    android:lines="11"
    android:paddingLeft="35dp"
    android:paddingTop="20dp"
    android:singleLine="false"
    android:inputType="textMultiLine" 
   />

7条回答
Lonely孤独者°
2楼-- · 2019-02-20 00:36

Add this in the layout:

android:maxLength="10" // If you want 10 symbols entered in the EditText

EDIT:

android:lines="10" // For the numer of lines in the EditText
查看更多
该账号已被封号
3楼-- · 2019-02-20 00:36

As you can see in above answers , you cant limit line length in edit text. But you can use text watcher that you mentioned to detect any changes in text. The override methods are self-explanatory in text watcher. Just make sure you dont edit, ie add text to the associated view when using text watcher. It will go in a recursive loop.

查看更多
贪生不怕死
4楼-- · 2019-02-20 00:41

Add this

android:maxLength="5"

查看更多
淡お忘
5楼-- · 2019-02-20 00:48

You can use following properties:-

 android:maxLength="5";

if want single line:-

     android:singleLine="true";

else for fix lines:-

     android:maxLines="5";
查看更多
欢心
6楼-- · 2019-02-20 00:52

The following filter adds a new line after every n characters:

public class AutoWrapFilter implements InputFilter {
    private final int mLineChars;

    public AutoWrapFilter(int pLineChars) {
        mLineChars = pLineChars;
    }

    @Override
    public CharSequence filter(CharSequence src, int srcStart, int srcEnd, Spanned dest, int destStart, int destEnd) {
        CharSequence original = dest.subSequence(0,destStart);
        CharSequence replacement = src.subSequence(srcStart,srcEnd);

        if(replacement.length() < 1){
            return null;
        }

        int lastLineCharIndex = -1;

        for (int j = destStart - 1; j >= 0; j--){
            if(original.charAt(j) == '\n'){
                lastLineCharIndex = j;
                break;
            }
        }

        int charsAfterLine = lastLineCharIndex < 0 ? original.length() : original.length() - lastLineCharIndex;

        StringBuilder sb = new StringBuilder();

        for (int k = 0; k < replacement.length(); k++){

            if(charsAfterLine == mLineChars+1){
                charsAfterLine = 0;
                sb.append('\n');
            }

            sb.append(replacement.charAt(k));
            charsAfterLine++;

        }


        return sb;
    }

    public static void applyAutoWrap(EditText et, int wrapLength){
        InputFilter[] filters = et.getFilters();
        InputFilter[] newFilters = Arrays.copyOf(filters, filters.length + 1);
        newFilters[filters.length] = new AutoWrapFilter(wrapLength);
        et.setFilters(newFilters);
    }
}

Usage:

public class MyActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText edit = (EditText) findViewById(R.id.edit);

        AutoWrapFilter.applyAutoWrap(edit,10);

    }

}

Result:

Sceenshot

Also, you can add more code to append new-line char at word beginning, to make it wrap by word.

查看更多
不美不萌又怎样
7楼-- · 2019-02-20 00:56

Set these parameters in the layout:

android:layout_width="30dip"
android:singleLine="false"
android:maxLines="5"

Experiment with the value of android:layout_width to find the value that fits your needs.

查看更多
登录 后发表回答