关于android中TextView和EditText同步的demo
目录
在书上看了个例子,代码如下:
et = (EditText) findViewById(R.id.editText1);
tv = (TextView)findViewById(R.id.textView1);
et.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
tv.setText(et.getText());
tv.invalidate();
//判断是哪种链接方式
Log.d(TAG, "判断开始");
Linkify.addLinks(tv, Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES|Linkify.PHONE_NUMBERS);
return false;
}
})
发现输入时不能实时同步,只有按回车或者返回时才能同步。那么如何实现真正的不同呢。在网上发现了下面的方法:
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
et.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count){
tv.setText(et.getText());
Linkify.addLinks(tv, Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES|Linkify.PHONE_NUMBERS);
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void afterTextChanged(Editable s) {}
});