实时编辑用户输入
是否可以在用户输入数据时自动将字符插入到 EditText
中?
即,如果用户输入一个长数字,例如 123456789012
,当他在编辑文本框中键入该数字时,是否有可能显示该数字,但每 4 个字符有一个破折号?
因此,当您输入上面的数字时,您会看到它被输入到 EditText
框中,但看起来像这样:1234-5678-9012。
目前我有一个应用程序,您可以在其中输入一个长数字,然后按一个按钮,它会为您插入破折号,但我很好奇是否可以在您键入时完成?
非常感谢您的帮助。
Is it possible to auto insert characters into an EditText
as the user inputs data?
I.e. if the user is entering a long number such as 123456789012
, is it possible for this number to appear as he is typing it in the edit text box, but with a dash every 4th character?
So as you type the number above you would see it being entered in the EditText
box but would look like this: 1234-5678-9012.
Currently I have an app where you can enter a long number and then press a button and it inserts the dashes for you, but I'm curious if it could be done as you type?
Many thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过标记 android,我认为您正在讨论 android editText,这样您就可以通过监听 TextChangedListener 来做到这一点,
编辑:用于退格
By tagging android, I think you are discussing about android editText, is so you can do it by listening the TextChangedListener,
EDITED: for backspace
为了解决这个问题,我编写了一个类“AutoAddTextWatcher”:
1. 自动在EditText中插入文本。
2. 在EditText中您设定的位置插入文本。
3.当文本长度大于1时,删除EditText中您设置的位置处的文本。
代码片段:
AutoAddTextWatcher 类
完整演示源:
https://github.com/henrychuangtw/AutoInsertEditText
to solve this issue, i write a class "AutoAddTextWatcher" :
1. Auto insert text into EditText.
2. insert text into EditText at positions you are setted.
3. delete text in EditText at positions you are setted, when text length bigger than 1.
code snippet :
AutoAddTextWatcher class
complete demo source :
https://github.com/henrychuangtw/AutoInsertEditText
这也可以,只有这个代码会在第三个字符后插入
" - "
。This will do as well, only this code will insert
" - "
after 3rd character.这就是我
在这里使用的,您可以将分隔符替换为您想要分隔数字的内容。
This is what I used
Here you replace delimiter with what you want to separate digits.
对于那些仍然面临退格键和多个连字符问题的人 -
For those still facing trouble with backspace and multiple hyphens -
在我的例子中,您可以通过更改文本来实现此目的
,我必须像这样格式化输入: xxx xxx-xxxx
我按照下面给出的方式完成:
结果在打字时非常动态。
输入- 1234567890
结果 - 123 456-7890
You can achieve this with on text changed
in my case, i have to format input like this : xxx xxx-xxxx
i done as given below:
and the result is very dynamic on the go while typing.
input- 1234567890
result - 123 456-7890