简体   繁体   English

addTextChangedListener() 是否仍在 Android api 中?

[英]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 the TextWatcher , 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上找不到addTextChangedListenerTextWatcher是那个接口从来没有这样的方法。 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.

相关问题 Android:addTextChangedListener崩溃应用 - Android: addTextChangedListener crash app Android:从 addTextChangedListener 更改为 setOnKeyListener 以使用 Backspace - Android:Changing from addTextChangedListener to setOnKeyListener to use Backspace Android Studio - EditText.addTextChangedListener无效 - Android Studio - EditText.addTextChangedListener not working java.lang.NullPointerException :在 android.widget.EditText.addTextchangedlistener 上 - java.lang.NullPointerException : on android.widget.EditText.addTextchangedlistener android.widget.EditText.addTextChangedListener 的 java.lang.NullPointerException - java.lang.NullPointerException for android.widget.EditText.addTextChangedListener NullPointerException / NPE由addTextChangedListener引起 - NullPointerException / NPE Caused by addTextChangedListener 无法解析addTextChangedListener - addTextChangedListener cannot be resolved 尝试在空对象引用上调用虚拟方法“void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)” - Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference EditText addTextChangedListener循环 - EditText addTextChangedListener loop 使用 addTextChangedListener 给出 NullPointerException - Using addTextChangedListener gives NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM