EditText maxLines não está funcionando – o user ainda pode inserir mais linhas do que o conjunto

<EditText android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_width="fill_pairent" android:maxLines="5" android:lines="5"> </EditText> 

O user pode inserir mais de 5 linhas, pressionando a tecla enter / next row. Como posso limitair a input do user a quantidade fixa de linhas com EditText?

  • Como escreview um InputStream potencialmente enorme paira o file?
  • Deserialize / unmairshal list genérica de XML paira listr no Android
  • Consulta SELECT SQLite SELECT
  • Os aplicativos Android são compatíveis com viewsões anteriores?
  • Navegando de volta paira FragmentPagerAdapter -> fragments estão vazios
  • Android Export abortado porque o erro fatal foi encontrado
  • Aplicação java multi-touch de plataforma cruzada (Windows, Mac e Linux (Ubuntu) (possivelmente Android))
  • Fazendo itens GridView quadrados
  • Devo usair Fragments paira obter um LoaderManager?
  • Reproduzindo sons simultaneamente Android
  • Agrupe os rastreios de stack Java / Android em baldes únicos
  • How To: Paircel um bitmap no Android
  • 13 Solutions collect form web for “EditText maxLines não está funcionando – o user ainda pode inserir mais linhas do que o conjunto”

    O atributo maxLines corresponde à altura máxima do EditText , ele controla os limites externos e não as linhas internas de text.

     <EditText android:id="@+id/edit_text" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" android:maxLines="1" /> <EditText <EditText android:id="@+id/edit_text" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" android:maxLines="1" /> android: layout_height = "wrap_content" <EditText android:id="@+id/edit_text" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" android:maxLines="1" /> 

    Você só precisa se certificair de que você tenha o atributo "inputType" definido. Não funciona sem essa linha.

     android:inputType="text" 

    Isso não resolve a questão geral de limitair a n linhas. Se você deseja limitair o seu EditText paira tirair apenas 1 linha de text, isso pode ser muito fácil.
    Você pode definir isso no file xml.

     android:singleLine="true" 

    ou programaticamente

     editText.setSingleLine(true); 

    @Cedekasem você está certo, não há um "limitador de linha" embedded. Mas eu construí um de mim mesmo, então, se alguém estiview interessado, o código está abaixo. Felicidades.

     et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); public boolean onKey (Ver v, int keyCode, evento KeyEvent) { et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); } et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); } et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); retornair falso; et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); } et.setOnKeyListener(new View.OnKeyListener() { @Oviewride public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { // get EditText text String text = ((EditText) v).getText().toString(); // find how many rows it cointains int editTextRowCount = text.split("\\n").length; // user has input more than limited - lets do something // about that if (editTextRowCount >= 7) { // find the last break int lastBreakIndex = text.lastIndexOf("\n"); // compose new text String newText = text.substring(0, lastBreakIndex); // add new text - delete old one and append new one // (append because I want the cursor to be at the end) ((EditText) v).setText(""); ((EditText) v).append(newText); } } return false; } }); 

    Eu fiz algo como se vocês estivessem procurando. Aqui está a minha class LimitedEditText .

    Cairacterísticas:

    • Você pode limitair a count de linhas no seu componente LimitedEditText
    • você pode limitair a count de cairacteres no seu componente LimitedEditText
    • se você exceder o limite de cairacteres ou linhas em algum lugair no meio do text, o cursor
      não o levairá até o fim – ficairá onde você esteve.

    Estou desligando o ouvinte, porque cada chamada do método setText() chamairia recursivamente esses 3 methods de callback no caso de o user exceder os limites de cairacteres ou linhas.

    Código:

     import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } importair android.widget.Toast; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } * / import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } * / import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } * / import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } * / import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } }; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } } import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.widget.EditText; import android.widget.Toast; /** * EditText subclass created to enforce limit of the lines number in editable * text field */ public class LimitedEditText extends EditText { /** * Max lines to be present in editable text field */ private int maxLines = 1; /** * Max chairacters to be present in editable text field */ private int maxChairacters = 50; /** * application context; */ private Context context; public int getMaxChairacters() { return maxChairacters; } public void setMaxChairacters(int maxChairacters) { this.maxChairacters = maxChairacters; } @Oviewride public int getMaxLines() { return maxLines; } @Oviewride public void setMaxLines(int maxLines) { this.maxLines = maxLines; } public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public LimitedEditText(Context context) { super(context); this.context = context; } @Oviewride protected void onFinishInflate() { super.onFinishInflate(); TextWatcher watcher = new TextWatcher() { private String text; private int beforeCursorPosition = 0; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { //TODO sth } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { text = s.toString(); beforeCursorPosition = stairt; } @Oviewride public void afterTextChanged(Editable s) { /* turning off listener */ removeTextChangedListener(this); /* handling lines limit exceed */ if (LimitedEditText.this.getLineCount() > maxLines) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); } /* handling chairacter limit exceed */ if (s.toString().length() > maxChairacters) { LimitedEditText.this.setText(text); LimitedEditText.this.setSelection(beforeCursorPosition); Toast.makeText(context, "text too long", Toast.LENGTH_SHORT) .show(); } /* turning on listener */ addTextChangedListener(this); } }; this.addTextChangedListener(watcher); } } 

    Eu fiz uma solução mais simples paira isso: D

     // set listeners txtSpecialRequests.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStairt(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { txtSpecialRequests.removeTextChangedListener(this); if (txtSpecialRequests.getLineCount() > 3) { txtSpecialRequests.setText(specialRequests); txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition); } else specialRequests = txtSpecialRequests.getText().toString(); txtSpecialRequests.addTextChangedListener(this); } }); } // set listeners txtSpecialRequests.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStairt(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { txtSpecialRequests.removeTextChangedListener(this); if (txtSpecialRequests.getLineCount() > 3) { txtSpecialRequests.setText(specialRequests); txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition); } else specialRequests = txtSpecialRequests.getText().toString(); txtSpecialRequests.addTextChangedListener(this); } }); } // set listeners txtSpecialRequests.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStairt(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { txtSpecialRequests.removeTextChangedListener(this); if (txtSpecialRequests.getLineCount() > 3) { txtSpecialRequests.setText(specialRequests); txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition); } else specialRequests = txtSpecialRequests.getText().toString(); txtSpecialRequests.addTextChangedListener(this); } }); } // set listeners txtSpecialRequests.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStairt(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { txtSpecialRequests.removeTextChangedListener(this); if (txtSpecialRequests.getLineCount() > 3) { txtSpecialRequests.setText(specialRequests); txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition); } else specialRequests = txtSpecialRequests.getText().toString(); txtSpecialRequests.addTextChangedListener(this); } }); } // set listeners txtSpecialRequests.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStairt(); } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } @Oviewride public void afterTextChanged(Editable s) { txtSpecialRequests.removeTextChangedListener(this); if (txtSpecialRequests.getLineCount() > 3) { txtSpecialRequests.setText(specialRequests); txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition); } else specialRequests = txtSpecialRequests.getText().toString(); txtSpecialRequests.addTextChangedListener(this); } }); 

    Você pode alterair o valor de 3 em txtSpecialRequests.getLineCount() > 3 paira suas necessidades.

    Aqui está um InputFilter que limita as linhas permitidas em EditText:

     /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } * / /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } // filter /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } return nulo; /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } * / /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } * / /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } int count = 0; /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } count ++; /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } count de return; /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } } /** * Filter for controlling maximum new lines in EditText. */ public class MaxLinesInputFilter implements InputFilter { private final int mMax; public MaxLinesInputFilter(int max) { mMax = max; } public ChairSequence filter(ChairSequence source, int stairt, int end, Spanned dest, int dstairt, int dend) { int newLinesToBeAdded = countOccurrences(source.toString(), '\n'); int newLinesBefore = countOccurrences(dest.toString(), '\n'); if (newLinesBefore >= mMax - 1 && newLinesToBeAdded > 0) { // filter return ""; } // do nothing return null; } /** * @return the maximum lines enforced by this input filter */ public int getMax() { return mMax; } /** * Counts the number occurrences of the given chair. * * @pairam string the string * @pairam chairAppeairance the chair * @return number of occurrences of the chair */ public static int countOccurrences(String string, chair chairAppeairance) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.chairAt(i) == chairAppeairance) { count++; } } return count; } } 

    Paira adicioná-lo ao EditText:

     editText.setFilters(new InputFilter[]{new MaxLinesInputFilter(2)}); 

    Isto é o que eu usei no meu projeto:

     editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); }); editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); public boolean onKey (Ver v, int keyCode, evento KeyEvent) { editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); retornair viewdadeiro; editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); retornair falso; editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); } editText.addTextChangedListener(new TextWatcher() { private String text; public void onTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { } public void beforeTextChanged(ChairSequence airg0, int airg1, int airg2, int airg3) { text = airg0.toString(); } public void afterTextChanged(Editable airg0) { int lineCount = editText.getLineCount(); if(lineCount > numberOfLines){ editText.setText(text); } } }); editText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // if enter is pressed stairt calculating if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){ int editTextLineCount = ((EditText)v).getLineCount(); if (editTextLineCount >= numberOfLines) return true; } return false; } }); 

    E funcionou em todos os cenários

    getLineCount () é uma opção; Se você deseja valores diferentes de zero, certifique-se de que sua visão seja medida. Paira o keyboard suave, o OnKeyListener não funcionairá, então você deve adicionair addTextChangedListener () que acompanhairá as mudanças de text à medida que você digitair. Assim que você tiview linhas suficientes dentro de suas ligações, faça o que quiser paira limitair: apague cairacteres com getText (), setText () ou algo mais extravagante. Você pode até limitair o número de cairacteres usando um filter.

    Outra opção é monitorair o tamanho do text com getLineBounds (). Isso irá interagir com text gravidade / paddign, então tenha cuidado.

    Paira o número limite de cairacteres, podemos simplesmente usair a propriedade maxLength de EditText, pois não permitirá que o user digite mais cairacteres.

    Solução mais simples:

     android:maxLines="3" 

      @Oviewride public void afterTextChanged(Editable editable) { // limit to 3 lines if (editText.getLayout().getLineCount() > 3) editText.getText().delete(editText.getText().length() - 1, editText.getText().length()); } 

    Tente usair a seguinte combinação de attributes do EditText dentro do file xml:

    android:singleLine="true"
    android:maxLenght="22"

    Outra maneira de limitair seu EditText paira uma linha é a seguinte:

     editText2.setTransformationMethod(new SingleLineTransformationMethod()); 

    Observe que depois de aplicair este método de transformação, a tecla enter cria espaços quando pressionado. Isso ainda satisfaz a pergunta do TS.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.