监听textview的变动

实现android.text.TextWatcher接口,该接口提供了三个文本更新过程中的回调方法。

调用TextView.addTextChangedListener()方法将TextWatcher注册到textView上去。



		@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
			
		}

1.字符计数

public class TextwatchActivity extends Activity implements TextWatcher{
	EditText text;
	int textCount; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        text = new EditText(this);
        text.addTextChangedListener(this);
        setContentView(text);
    }
	@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) {
		textCount = text.getText().length();
		setTitle(String.valueOf(textCount)); 
	}
}




你可能感兴趣的:(用户界面攻略)