TextView input word count and restrictions in Android

Source: Internet
Author: User

When developing apps for Android, enter up to 140 words in the text edit box, often showing how many words are left to limit the number of words the user enters.

EditText content;//Define a text input box

TextView hasnum;//used to display the remaining words

int num = maximum number of words 140;//limit

Content = (EditText) Findviewbyid (r.id.et_content);

HASNUMTV = (TextView) Findviewbyid (r.id.tv_num);

Hasnumtv.settext (num+ "");

The following is a EditText text box to add listening

Content.addtextchangedlistener (New Textwatcher () {
Private Charsequence temp;
private int selectionstart;
private int selectionend;

public void beforetextchanged (Charsequence s, int. start, int count, int after) {


}

public void ontextchanged (charsequence s, int start, int before, int count) {
temp = s;
}

public void aftertextchanged (Editable s) {
int number = Num-s.length ();
Hasnumtv.settext ("" + number);
SelectionStart = Content.getselectionstart ();
Selectionend = Content.getselectionend ();
if (temp.length () > num) {
S.delete (SelectionStart-1, selectionend);
int tempselection = Selectionend;
Content.settext (s);
Content.setselection (tempselection);//Set the cursor at the end
}
}
});

TextView input word count and restrictions in Android

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.