Method one. Using the View.onkeylistener "sync" display
Java code
- EditText Myedit = (EditText) Findviewbyid (R.id.myedit);
- TextView MyText = (TextView) Findviewbyid (R.id.mytext);
- Myedit.setonkeylistener (new Edittext.onclicklistener () {
- Public Boolean OnKey (View V, int keycode, keyevent event)
- {
- Mytext.settext (Myedit.gettext ());
- return false;
- }
- });
Method Two. True synchronization with Textwatcher
Java code
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.text.Editable;
- Import Android.text.TextWatcher;
- Import Android.widget.EditText;
- Import Android.widget.TextView;
- Public class Textwatcherdemo extends Activity implements textwatcher{
- /** Called when the activity is first created. * /
- private TextView MyText;
- private EditText Myedit;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- mytext= (TextView) Findviewbyid (R.id.text);
- myedit= (EditText) Findviewbyid (R.id.edit);
- Myedit.addtextchangedlistener (this);
- }
- @Override
- public void aftertextchanged (Editable s) {
- //TODO auto-generated method stub
- }
- @Override
- public void beforetextchanged (charsequence s, int start, int count,
- int after) {
- //TODO auto-generated method stub
- }
- @Override
- public void ontextchanged (charsequence s, int start, int before, int count) {
- //TODO auto-generated method stub
- Mytext.settext (Myedit.gettext ());
- }
- }
Android Tips-TextView and edittext sync display