Set maximum number of text lines for an EditText

前端 未结 6 1332
情书的邮戳
情书的邮戳 2020-12-18 23:51

Is there a way of specifying the maximum numbers of lines for an EditText? By that I mean all the lines of text, not only the visible ones (as the android

相关标签:
6条回答
  • 2020-12-18 23:57
    android:maxLength="140" 
    

    I believe this is equal to one line, in theory you could change the maxlength to "700" which would be 5 lines. I'm not exactly sure on the numbers but the logic behind it is valid.

    0 讨论(0)
  • 2020-12-18 23:58

    You could use this:

    android:singleLine="false" android:lines="5"
    

    I don't know if that does what you need.

    0 讨论(0)
  • 2020-12-18 23:58
    <EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:maxLines="1" />
    

    You just need to make sure you have the attribute "inputType" set. It doesn't work without this line.

    android:inputType="text"

    0 讨论(0)
  • 2020-12-19 00:02

    set android:maxLines="2"in xml and add :

    edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
    
        @Override
        public void afterTextChanged(Editable editable) {
            if (null != edittext.getLayout() && edittext.getLayout().getLineCount() > 2) {
               edittext.getText().delete(edittext.getText().length() - 1, edittext.getText().length());
            }
        }
    });
    

    is solution for me . The same you can do for 3,4 ... n rows limit for your edittext.

    0 讨论(0)
  • 2020-12-19 00:02
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    
    public class EditTextLinesLimiter implements TextWatcher {
        private EditText editText;
        private int maxLines;
        private String lastValue = "";
    
        public EditTextLinesLimiter(EditText editText, int maxLines) {
            this.editText = editText;
            this.maxLines = maxLines;
        }
    
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            lastValue = charSequence.toString();
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
            if (editText.getLineCount() > maxLines) {
                int selectionStart = editText.getSelectionStart() - 1;
                editText.setText(lastValue);
                if (selectionStart >= editText.length()) {
                    selectionStart = editText.length();
                }
                editText.setSelection(selectionStart);
            }
        }
    }
    

    And then:

    editText.addTextChangedListener(new EditTextLinesLimiter(editText, 2));
    
    0 讨论(0)
  • 2020-12-19 00:06

    I believe you can use this code to check the number of lines in edittext,

    editText.getLayout().getLineCount() > row-limit;
    

    Preferably you may use it in a TextWatcher Listener's "afterTextChanged()" to disallow the user for entering further if the row limit has been reached.

    edit.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            if(editText.getLayout().getLineCount() > row-limit){
               //Code to stop edittext from being edited any further.
              }
    });
    
    0 讨论(0)
提交回复
热议问题