Android EditText intercept and listen for input events

Source: Internet
Author: User

There are 2 ways that Android EditText intercepts and listens for input events:
1. The first method: Use Setonkeylistener (), but this way you can only listen to hard keyboard events.

  1. EditText. Setonkeylistener(new View). Onkeylistener() {
  2. @Override
  3. Public Boolean onKey(View v, int keycode, keyevent Event) {
  4. TextView. SetText(edittext. GetText());
  5. Return false;
  6. }
  7. });

2. The second method: using the Textwatcher class, this way is able to listen to the soft keyboard and the hard keyboard, we only need to implement the OnTextChanged method, in addition The Textwatcher also provides beforetextchanged and Aftertextchanged methods for more detailed input listening processing.

  1. EditText. Addtextchangedlistener(new textwatcher() {
  2. @Override
  3. Public void ontextchanged(charsequence s, int start, int before, int count) {
  4. TextView. SetText(edittext. GetText());
  5. }
  6. @Override
  7. Public void beforetextchanged(charsequence s, int start, int count, int after) {
  8. }
  9. @Override
  10. Public void aftertextchanged(Editable s) {
  11. }
  12. });

Code snippet: The implementation is not allowed to enter when input to the maximum value, in addition, because EditText does not provide us with EditText's forbidden input function, the following method also implements this function.

  1. Private void seteditable(EditText medit, int maxLength, Boolean value) {
  2. If (value) {
  3. Medit. SetFilters(new inputfilter[] { new myeditfilter( MaxLength) });
  4. Medit. Setcursorvisible(true);
  5. Medit. Setfocusableintouchmode(true);
  6. Medit. Requestfocus();
  7. }
  8. else {
  9. Medit. SetFilters(new inputfilter[] { new inputfilter() {
  10. @Override
  11. public charsequence (charsequence Source int Start,int end, spanned Dest, int Dstart, int DEnd { /span>
  12. return source. Length() < 1 dest. Subsequence(dstart, dend) : " ";
  13. }
  14. } });
  15. Medit. Setcursorvisible(false);
  16. Medit. Setfocusableintouchmode(false);
  17. Medit. Clearfocus();
  18. }
  19. }

Android EditText intercept and listen for input events

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.