getText vs onTextChanged charSequence

时光毁灭记忆、已成空白 提交于 2020-02-08 00:54:08

问题


I am learning how to write applications for android and have a question. What is the difference between using a TextWatcher and within the the onTextChanged Method, setting a string value equal to the CharSequence argument and simply using getText method.

private TextWatcher passwordListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            try{
                password = charSequence.toString();
            }

            catch(Exception e){
                password=null;
            }
        }

vs

password = password_EditText.getText().toString();

回答1:


You would use a text watcher if you want to be notified that the text has been changed and need to do something in direct response to that event itself. For example if you wanted to do some custom validation on the fly and automatically enable / disable a button based on the value entered.

Using getText just simply returns what ever is in the edit text field at that particular time. You might use this if you want to get the text value as a result of some other event like a button click.




回答2:


TextWatcher can dynamic listener text changge , more Powerful text processing。 getText() is static get.



来源:https://stackoverflow.com/questions/21358915/gettext-vs-ontextchanged-charsequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!