Android TextWatcher内容监听死循环 Android TextWatcher内容监听死循环案例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android TextWatcher内容监听死循环 Android TextWatcher内容监听死循环案例详解

自己找知己   2021-08-30 我要评论
想了解Android TextWatcher内容监听死循环案例详解的相关内容吗,自己找知己在本文为您仔细讲解Android TextWatcher内容监听死循环的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,TextWatcher监听,Android,TextWatcher监听死循环,下面大家一起来学习吧。

Android TextWatcher内容监听死循环

TextWatcher如何避免在afterTextChanged中调用setText后导致死循环,今天在用TextView时,添加了addTextChangedListener方法监听内容改变,在afterTextChanged方法中又执行了setText方法,结果造成afterTextChanged方法再次调用,然后setText,因此造成了死循环的问题。列出此问题,以备后忘。

先贴Google文档原文说明:

/** * This method is called to notify you that, somewhere within * <code>s</code>, the text has been changed. * It is legitimate to make further changes to <code>s</code> from * this callback, but be careful not to get yourself into an infinite * loop, because any changes you make will cause this method to be * called again recursively. * (You are not told where the change took place because other * afterTextChanged() methods may already have made other changes * and invalidated the offsets. But if you need to know here, * you can use {@link Spannable#setSpan} in {@link #onTextChanged} * to mark your place and then look up from here where the span * ended up. */public void afterTextChanged(Editable s);

根据文档说明意思就是调用setText之前暂时去掉此监听器, 然后再恢复添加自身即可.

如下:

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们