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条回答
Deceive 欺骗
2楼-- · 2019-02-20 01:03
String s="asaaskkjskjkajskjkajskajksjkfldfassasaasssssssssa.";
        int l=s.length();
        String y = "";
       for(int i=0;i<l;i+=20){
        int z=20;
        if(s.length()<z){
            z=s.length();
        }
        y=y+s.substring(0, z)+"\n";
        s=s.substring(z);
    }
    ((EditText)findViewById(R.id.editText1)).setText(y);
查看更多
登录 后发表回答