Android 小技巧-- TextView与EditText 同步显示
方法一.利用View.OnKeyListener"同步"显示
- 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,真正实现同步
- 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());
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。