TextWatcher是一个文本框的监听器,直接举一个例子,从我工程的代码中复制的,是一个在编辑框输入内容的时候(内容是可以变化的),下面的文本框显示从数据库中查到的内容。
public class AddressActivity extends Activity {private EditText etNumber;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
etNumber=(EditText)findViewById(R.id.et_number);
tvResult=(TextView)findViewById(R.id.tv_result);
//此处调用了了文本框监听器
etNumber.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
String address = AddresDao.getAddress(s.toString());
tvResult.setText(address);
}
// 文本框变化前
@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
//在文本框变化之后
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String address = AddresDao.getAddress(s.toString());
tvResult.setText(address);
}
});
}
}