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

Android如何设置键盘显示指定字符?移除逗号按键实现方法

Android中让EditText键盘完全隐藏逗号的实现方法

要让逗号完全不在输入法键盘上显示,核心是通过android:digits属性精准定义EditText允许输入的所有字符——输入法会自动只展示该属性包含的字符,未列入的字符(比如逗号)会直接从键盘中移除,而非仅置灰不可用。

具体实现

你的现有代码已经具备核心逻辑,只需确保android:digits属性中不包含逗号即可,配合android:inputType限定输入类型,就能实现需求:

<EditText
    android:id="@+id/MyEd"
    android:layout_x="53dp"
    android:layout_y="80dp"
    android:layout_width="192dp"
    android:textSize="20sp"
    android:layout_height="40dp"
    android:paddingLeft="10dp"
    android:text=""
    android:singleLine="true"
    android:enabled="true"
    android:focusableInTouchMode="true"
    android:cursorVisible="true"
    android:focusable="true"  
    android:textColor="@drawable/ed_text_selector"
    android:background="@drawable/ed_text_rounded"
    android:inputType="number"
    android:digits="0123456789." <!-- 仅包含数字和小数点,逗号不在此列表中 -->
    android:maxLength="25"
    android:selectAllOnFocus="true"/>

关键属性说明

  • android:inputType="number":限定输入类型为数字,触发输入法弹出数字样式键盘
  • android:digits="0123456789.":明确指定允许输入的字符集合,输入法会严格按照该集合展示按键,不在集合内的逗号会被完全隐藏

设置完成后,输入法键盘仅会显示数字0-9和小数点,逗号既不会出现在键盘上,也无法被输入到EditText中。

内容的提问来源于stack exchange,提问作者Максим Шовкун

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:52:37