方法一.利用View.OnKeyListener"同步"显示
Java代码
class="star" src="/Upload/Images/2014112115/40B102E0EF997EA6.png" alt="收藏代码" />
- 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{
-
- 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) {
-
-
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
-
-
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
-
- myText.setText(myEdit.getText());
- }
-
- }