如何在Android中使用TextWatcher类?
谁能告诉我如何屏蔽 EditText
中的 子字符串 或如何更改 EditText
子字符串输入为密码类型 或替换为另一个字符,例如 123xxxxxxxxx3455
String contents = et1.getText().toString();
et1.setText(contents.replace.substring(0, contents.length()-2),"*");
请告诉我如何在 Android 中使用 TextWatcher
方法。
Can anyone tell me how to mask the substring in EditText
or how to change EditText
substring input to password type or replace by another character like this 123xxxxxxxxx3455
String contents = et1.getText().toString();
et1.setText(contents.replace.substring(0, contents.length()-2),"*");
Please, tell me how I can use the TextWatcher
method in Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对于
TextWatcher
的使用...For use of the
TextWatcher
...TextWatcher
接口有 3 个回调方法,当文本发生更改时,这些方法都会按以下顺序调用:s
参数是应用任何更改之前的文本。start
参数是文本中更改部分开始的位置。count
参数是自start
位置以来s
序列中已更改部分的长度。after
参数是新序列的长度,它将替换s
序列中从start
开始的部分到开始+计数
。您不得更改从此方法中
TextView
中的文本(通过使用myTextView.setText(String newText)
)。beforeTextChanged
方法类似,但在文本更改之后调用。s
参数是应用更改后的文本。start
参数与beforeTextChanged
方法中的参数相同。count
参数是 beforeTextChanged 方法中的after
参数。而
before
参数就是beforeTextChanged方法中的count
参数。您不得更改从此方法中
TextView
中的文本(通过使用myTextView.setText(String newText)
)。TextView
中的文本。/!\ 警告: 当您更改
TextView
中的文本时,TextWatcher
将再次触发,从而开始无限循环。然后,您应该添加一个boolean _ignore
属性来防止无限循环。示例:
摘要:
一个随时可用的类:
TextViewListener
就我个人而言,我制作了自定义文本侦听器,它为我提供了单独字符串中的 4 个部分,对我来说,使用起来更加直观。
例子:
The
TextWatcher
interface has 3 callbacks methods which are all called in the following order when a change occurred to the text:The
s
parameter is the text before any change is applied.The
start
parameter is the position of the beginning of the changed part in the text.The
count
parameter is the length of the changed part in thes
sequence since thestart
position.And the
after
parameter is the length of the new sequence which will replace the part of thes
sequence fromstart
tostart+count
.You must not change the text in the
TextView
from this method (by usingmyTextView.setText(String newText)
).beforeTextChanged
method but called after the text changes.The
s
parameter is the text after changes have been applied.The
start
parameter is the same as in thebeforeTextChanged
method.The
count
parameter is theafter
parameter in the beforeTextChanged method.And the
before
parameter is thecount
parameter in the beforeTextChanged method.You must not change the text in the
TextView
from this method (by usingmyTextView.setText(String newText)
).TextView
from this method./!\ Warning: When you change the text in the
TextView
, theTextWatcher
will be triggered again, starting an infinite loop. You should then add like aboolean _ignore
property which prevent the infinite loop.Exemple:
Summary:
A ready to use class:
TextViewListener
Personally, I made my custom text listener, which gives me the 4 parts in separate strings, which is, for me, much more intuitive to use.
Example:
补充答案
这是对其他答案的视觉补充。我对代码和解释的更完整的回答是这里。
< img src="https://i.sstatic.net/yhv4A.gif" alt="在此处输入图像描述">
Supplemental answer
Here is a visual supplement to the other answers. My fuller answer with the code and explanations is here.
对于 Kotlin 使用 KTX 扩展 函数:
(它使用
TextWatcher
作为之前的答案)导入 core-KTX:
For Kotlin use KTX extension function:
(It uses
TextWatcher
as previous answers)import
core-KTX
:解决方案的更大视角:
这对我有用,我第一次这样做。
A little bigger perspective of the solution:
This works for me, doing it my first time.
在 Android 中使用 TextWatcher
以下是示例代码。尝试使用 TextView 的
addTextChangedListener
方法Using TextWatcher in Android
Here is a sample code. Try using
addTextChangedListener
method of TextView创建自定义 TextWatcher 子类:
为您的 EditText 设置侦听器:
Create custom TextWatcher subclass:
Set listener for your EditText:
如果你用对话框edittext来实现。像这样使用:与其他编辑文本的使用相同。
if you implement with dialog edittext. use like this:. its same with use to other edittext.
我不想实现 TextWatcher 的所有方法,因为我创建了一个抽象类 TextObserver ,问题就解决了:
I don't want to implement all methods of the
TextWatcher
to I created an abstract ClassTextObserver
and voila problem solved:我不想重写/实现
TextWatcher
的所有方法,并将stub
代码放入我不使用的方法中,以便让事情变得简单:-) 我创建了一个抽象类TextObserver
,瞧,问题解决了。I don't want to override/implement all methods of the
TextWatcher
and putstub
code in methods I don't use so to keep things simple :-) I created an abstract ClassTextObserver
and voila problem solved.