[英]Is addTextChangedListener() still in the Android api?
I am using a book from 2013, Android How To Program 2 nd Edition by Deitel and Harvey, they are using a method addTextChangedListener()
to set up the listener for the TextWatcer
, is this method still in the api?我正在使用 2013 年的一本书, Android How To Program 2 nd Edition by Deitel 和 Harvey,他们使用方法addTextChangedListener()
来设置TextWatcer
的侦听器,这个方法还在 api 中吗?
I can't find any info on it on the Android developer doc.我在 Android 开发人员文档中找不到任何相关信息。
//set amountEditText's TextWatcher
EditText amountEditText=(EditText) findViewById(R.id.amountEditText);
amountEditText.addTextChangedListener(amountEditTextWatcher);
is this method still in the api这个方法还在api中吗
Yes, you inherit it from TextView
, the superclass of EditText
.是的,您从EditText
的超类TextView
继承它。
I am using a book from 2013, Android How To Program 2nd Edition by Deitel and Harvey, they are using a method
addTextChangedListener()
to set up the listener for theTextWatcher
, is this method still in the API?我正在使用 2013 年的一本书, Android How To Program 2nd Edition by Deitel 和 Harvey,他们使用方法addTextChangedListener()
来设置TextWatcher
的侦听器,这个方法还在 API 中吗?
The code that you quoted in your question is calling addTextChangedListener
on amountEditText
which is an EditView
object.您在问题中引用的代码是在amountEditText
上调用addTextChangedListener
,这是一个EditView
object。 In other words, it is setting the amountEditTextWatcher
as the text watcher for the EditView
... like the comment says.换句话说,它将amountEditTextWatcher 设置为amountEditTextWatcher
的文本观察EditView
......就像评论所说的那样。
You don't set a listener for a watcher.您没有为观察者设置监听器。 A watcher is a kind of listener.观察者是一种倾听者。 You set a listener / watcher on (for example) a view.您在(例如)视图上设置监听器/观察器。
So basically, the code in the book is not doing what you think.所以基本上,书中的代码并没有按照你的想法做。
And the reason that you can't find addTextChangedListener
on TextWatcher
is that that interface has never had such a method.而在TextWatcher上找不到addTextChangedListener
的TextWatcher
是那个接口从来没有这样的方法。 It is a method on EditView
inherited from TextView
.它是从EditView
继承的TextView
上的一个方法。
Yes, you can see this sample.是的,你可以看到这个样本。
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
System.out.println(cs); //This is your live editing text.
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
}
@Override
public void afterTextChanged(Editable arg0) {
Toast.makeText(getApplicationContext(),"after text change",Toast.LENGTH_LONG).show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.