简体   繁体   English

在数字输入中添加特殊字符

[英]Adding special characters to number input

I'm trying to create a HTML duration(not time of day) input field, ie HH:MM, and I would like it to trigger the numeric keyboard on mobile devices. 我正在尝试创建一个HTML持续时间(不是一天中的时间)输入字段,即HH:MM,我希望它触发移动设备上的数字键盘。

<input type="number" /> appears to be the only reliable way to make this happen. <input type="number" />似乎是实现此目标的唯一可靠方法。 But I require it to accept colons : . 但是我要求它接受冒号:

Is there a way to force the number input field accept additional special characters? 有没有一种方法可以强制数字输入字段接受其他特殊字符?

As far as I know, it's not possible. 据我所知,这是不可能的。 As you need the numeric keyboard on mobile device along with : is not native at all. 由于您需要移动设备上的数字键盘以及:根本不是本地的。 There is just no : key in the keyboard, nothing you can do about it. 键盘上没有:键,您对此无能为力。

Just use <input type=time> and you can make magic happen nicely on mobile device. 只需使用<input type=time> ,您就可以使魔术在移动设备上发生得很好。 If you need the user to type in, use number or text , and add additional pattern property to perform the validation. 如果您需要用户输入,请使用numbertext ,并添加其他pattern属性以执行验证。 Or use something like jQuery validation plugin to perform just in time validation. 或使用类似jQuery验证插件的工具来及时执行验证。

Try for HH:MM entry and H:MM entry 尝试输入HH:MM和H:MM

<EditText>
     android:digits="1234567890:"
     android:inputType="textNosuggestions" 
     android:minLength="4"
     android:maxLength="5"/>

If you want to prevent users from entering time like "91:87", or ":36" or ":3:4" and all such variations then you would have to use a TextWatcher like this edistext.addTextChangedListener(new TextWatcher(this)); 如果要防止用户输入“ 91:87”,“:36”或“:3:4”之类的时间以及所有此类变体,则必须使用像edistext.addTextChangedListener(new TextWatcher(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM