EditText上怎么能只接受整型或浮点值? [英] How can EditText accept only integer or float value?
问题描述
我是新的Android开发。在我的项目,我使用的EditText,但我想迫使它只能接受一个整数或浮点数。我该怎么办呢?
I am new to Android development. In my project, I’m using EditText, but I want to force it to only accept an integer or float value. How can I do that?
推荐答案
如果你想使用十进制数只在您的EditText
If you want to use Decimal Number only on your EditText
使用XML属性安卓inputType =numberDecimal
在你的EditText小部件
您的EditText宣言将是这样的:
use the xml attribute android:inputType="numberDecimal"
in your EditText widget
your EditText declaration will be like this:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
如果你想使用符号十进制数比将二者结合起来的XML属性安卓inputType =numberDecimal
和 Android的:inputType =numberSigned
。您的EditText宣言将是这样的:
If you want to use Signed Decimal Number than combine the two Xml attributes android:inputType="numberDecimal"
and android:inputType="numberSigned"
. Your EditText declaration will be like this:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal|numberSigned" >
</EditText>
这篇关于EditText上怎么能只接受整型或浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!