Android --- Three ways to limit the number of words entered by EditView, androideditview

Source: Internet
Author: User

Android --- Three ways to limit the number of words (reprinted) in EditView, androideditview

Method 1: Use TextWatcher

Java code  
  1. EditText. addTextChangedListener (new TextWatcher (){
  2. Private CharSequence temp;
  3. Private boolean isEdit = true;
  4. Private int selectionStart;
  5. Private int selectionEnd;
  6. @ Override
  7. Public void beforeTextChanged (CharSequence s, int arg1, int arg2,
  8. Int arg3 ){
  9. Temp = s;
  10. }
  11. @ Override
  12. Public void onTextChanged (CharSequence s, int arg1, int arg2,
  13. Int arg3 ){
  14. }
  15. @ Override
  16. Public void afterTextChanged (Editable s ){
  17. SelectionStart = editText. getSelectionStart ();
  18. SelectionEnd = editText. getSelectionEnd ();
  19. Log. I ("gongbiao1", "" + selectionStart );
  20. If (temp. length ()> Constant. TEXT_MAX ){
  21. Toast. makeText (KaguHomeActivity. this,
  22. R. string. edit_content_limit, Toast. LENGTH_SHORT)
  23. . Show ();
  24. S. delete (selectionStart-1, selectionEnd );
  25. Int tempSelection = selectionStart;
  26. EditText. setText (s );
  27. EditText. setSelection (tempSelection );
  28. }
  29. }
  30. });

 

Method 2: Use InputFilter

Java code  
  1. EditText. setFilters (new InputFilter [] {new InputFilter. LengthFilter (100)}); // 100 of the maximum input words

 

Method 3: Set in XML

Xml Code  
  1. <EditText
  2. .
  3. .
  4. .
  5. Android: maxLength = "100"
  6. />

From the MS Platform, some things are really not used to their own


How does one limit the number of words in textview in android?

When developing applications, users are often limited to the number of words they enter, such as posting comments or other things. Below is a simple demo.

EditText et_content; // define a text input box <br> TextView TV _num; // display the remaining words <br> int num = 10; // maximum number of words <br>

Et_content = (EditText) findViewById (R. id. et_content); <br> TV _num = (TextView) findViewById (R. id. TV _num); <br> TV _num.setText ("10"); <br>

Add a listener for the EditText text box below

Et_content.addTextChangedListener (new TextWatcher (){
Private CharSequence temp;
Private int selectionStart;
Private int selectionEnd;

@ Override
Public void onTextChanged (CharSequence s, int start, int before,
Int count ){
Temp = s;
System. out. println ("s =" + s );
}

@ Override
Public void beforeTextChanged (CharSequence s, int start, int count,
Int after ){
}

@ Override
Public void afterTextChanged (Editable s ){
Int number = num-s. length ();
TV _num.setText ("" + number );
SelectionStart = et_content.getSelectionStart ();
SelectionEnd = et_content.getSelectionEnd ();
// System. out. println ("start =" + selectionStart + ", end =" + selectionEnd );
If (temp. length ()> num ){
S. delete (selectionStart-1, selectionEnd );
... The remaining full text>

How can I limit the number of characters in android EditText?

EditText inherits from TextView and can use TextView attributes.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.