Android Tips-TextView and edittext sync display

Source: Internet
Author: User
Tags gettext

Method one. Using the View.onkeylistener "sync" display

Java code
    1. EditText Myedit = (EditText) Findviewbyid (R.id.myedit);
    2. TextView MyText = (TextView) Findviewbyid (R.id.mytext);
    3. Myedit.setonkeylistener (new Edittext.onclicklistener () {
    4. Public Boolean OnKey (View V, int keycode, keyevent event)
    5. {
    6. Mytext.settext (Myedit.gettext ());
    7. return false;
    8. }
    9. });

Method Two. True synchronization with Textwatcher

Java code
  1. Import android.app.Activity;
  2. Import Android.os.Bundle;
  3. Import android.text.Editable;
  4. Import Android.text.TextWatcher;
  5. Import Android.widget.EditText;
  6. Import Android.widget.TextView;
  7. Public class Textwatcherdemo extends Activity implements textwatcher{
  8. /** Called when the activity is first created. * /
  9. private TextView MyText;
  10. private EditText Myedit;
  11. @Override
  12. public void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (R.layout.main);
  15. mytext= (TextView) Findviewbyid (R.id.text);
  16. myedit= (EditText) Findviewbyid (R.id.edit);
  17. Myedit.addtextchangedlistener (this);
  18. }
  19. @Override
  20. public void aftertextchanged (Editable s) {
  21. //TODO auto-generated method stub
  22. }
  23. @Override
  24. public void beforetextchanged (charsequence s, int start, int count,
  25. int after) {  
  26. //TODO auto-generated method stub
  27. }
  28. @Override
  29. public void ontextchanged (charsequence s, int start, int before, int count) {
  30. //TODO auto-generated method stub
  31. Mytext.settext (Myedit.gettext ());
  32. }
  33. }

Android Tips-TextView and edittext sync display

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.