You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Codename One实现仅数字且掩码的PIN输入框问题

解决PIN输入框仅数字且掩码的问题

你的代码问题在于连续调用两次.constraint()方法,后一次调用会覆盖前一次的设置,导致只有TextArea.PASSWORD约束生效,TextArea.NUMERIC约束被忽略,所以输入框仍能接受非数字字符。

要同时启用数字限制和密码掩码,需要将两个约束通过**按位或运算符(|)**组合起来——因为TextArea的约束是基于位掩码的整数常量,支持多约束叠加。

修改后的代码如下:

TextComponent currentPIN = new TextComponent()
    .labelAndHint("Current Pin")
    .constraint(TextArea.NUMERIC | TextArea.PASSWORD);
TextComponent newPIN = new TextComponent()
    .labelAndHint("New PIN")
    .constraint(TextArea.NUMERIC | TextArea.PASSWORD);
TextComponent confirmPIN = new TextComponent()
    .labelAndHint("Confirm PIN")
    .constraint(TextArea.NUMERIC | TextArea.PASSWORD);

这样设置后,输入框会同时限制仅接受数字输入,并且以密码形式掩码显示输入内容。

内容的提问来源于stack exchange,提问作者Festus Mutinda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:10:54