Live editing of users input(实时编辑用户输入)
问题描述
是否可以在用户输入数据时自动将字符插入到 EditText ?
Is it possible to auto insert characters into an EditText as the user inputs data?
即如果用户输入一个长数字,例如 123456789012,这个数字是否可以在他在编辑文本框中输入时出现,但每 4 个字符有一个破折号?
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?
因此,当您键入上面的数字时,您会看到它被输入到 EditText 框中,但看起来像这样:1234-5678-9012.
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?
非常感谢您的帮助.
推荐答案
通过标记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,
已用于退格
editText.addTextChangedListener(new TextWatcher() {
int len=0;
@Override
public void afterTextChanged(Editable s) {
String str = editText.getText().toString();
if(str.length()==4&& len <str.length()){//len check for backspace
editText.append("-");
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
String str = editText.getText().toString();
len = str.length();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
这篇关于实时编辑用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实时编辑用户输入
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
