当我将输入类型属性设置为数字(Android:输入类型=“数字”)并设置数字属性时,就像这个Android:数字=“0123456789”。输入事件按预期正常工作。即仅以数字形式接收输入。参考屏幕截图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:digits="0123456789"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
</LinearLayout>
当我将inputType属性设置为text person name(Android:input type = " text person name ")并像这样设置digits属性android:digits="0123456789 "。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:digits="0123456789"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp" />
</LinearLayout>
输入事件未按预期正常工作。那就是,
这是什么原因呢?
您可以对编辑文本中特定字符使用输入过滤器,您可以只允许您想要显示的字符。
点击此链接查看我的回答:
如何使用输入过滤器来限制Android版编辑文本中的字符?
一旦看起来像是设备特定的问题,就尝试这样做。
带有文本输入类型的数字是无效的组合。digits表示将假定数字类型。
至于奇怪的输入——我猜是键盘和编辑文本之间对文本完成状态的误解。当编辑文本试图覆盖我输入的内容时,我在编写键盘时看到了类似的错误——它导致文本完成被提交,因为它看起来像光标移动了。Android键盘API真的需要大修了。
android:digits="0123456789"
这个属性到底是做什么的?
无论你指定什么输入类型为< code>android:inputType都没关系。唯一重要的是这个< code>EditText将接受哪些数字。如果您这样设置,它指定这个有一个数字输入方法,并且这些特定的字符是它将接受的字符,在您的情况下是(0-9)。并且numeric被暗示为真。默认值为< code>false。
因此,如果你不想发生这种情况,你应该删除这条线。