Android -- TextView与EditText 同步显示
文章分类:JavaEye
方法一.利用View.OnKeyListener"同步"显示
   
 
 
Java代码 
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; 

}); 
方法二.  利用TextWatcher,真正实现同步
Java代码 
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()); 
    }