如何将EditText输入限制为数字(可能是十进制和带符号)输入?

时间:2021-03-07 00:06:09

I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.

我读过Android:将EditText限制为数字,如何在android中的EditText上显示数字键盘?不幸的是,它们似乎都不符合我的需求。

I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.

我想将EditText输入限制为仅限数字。但是,我还想允许签名和/或十进制输入。

Here is my current code (I need to do this programmatically):

这是我当前的代码(我需要以编程方式执行此操作):

EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);

With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.

有了这个,我的EditText快乐地将所有输入限制为数字。不幸的是,它不允许任何其他内容,如小数点。

If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL), the EditText accepts all input (which isn't what I want...).

如果我将该行更改为edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL),则EditText会接受所有输入(这不是我想要的...)。

I've tried combining flags (in desperation to see if it would work):

我尝试过组合旗帜(绝望地看看它是否会起作用):

edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)

That didn't work either (the EditText accepted all input as usual).

这也不起作用(EditText像往常一样接受所有输入)。

So, how do I do this?

那么,我该怎么做?

8 个解决方案

#1


53  

Try using TextView.setRawInputType() it corresponds to the android:inputType attribute.

尝试使用TextView.setRawInputType()它对应于android:inputType属性。

#2


147  

There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:

没有理由使用setRawInputType(),只需使用setInputType()。但是,您必须将类和标志与OR运算符组合在一起:

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

#3


12  

The best way to do that programmatically is using the next method:

以编程方式执行此操作的最佳方法是使用下一个方法:

public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 

Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

返回一个DigitsKeyListener,它接受数字0到9,加上减号(仅在开头)和/或小数点(每个字段只有一个),如果指定的话。

This solve the problem about the many '.' in EditText

这解决了很多'''的问题。在EditText中

editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers. 

editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.

#4


9  

put this line in xml

把这行放在xml中

 android:inputType="number|numberDecimal"

#5


5  

Use this. Works fine

用这个。工作正常

input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

#6


4  

Hi All I also had this problem. I use C# with Xamarin

大家好我也有这个问题。我使用C#和Xamarin

Just a slight note that I hope someone else as well.

只是略微注意,我希望其他人也是。

I have tried multiple of the methods mentioned here.

我尝试了这里提到的多种方法。

edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);

was the closest but still did not work. As Ralf mentioned, you could use

是最接近但仍然没有奏效。正如拉尔夫所说,你可以使用

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

However in my C# I did not have this. instead you do it as follow:

但是在我的C#中我没有这个。相反,你这样做如下:

edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;

Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!

请注意,这些的顺序很重要。如果你首先放置十进制,它仍然允许你键入任何字符,其中如果Numer是第一个它只是数字,但允许小数分隔符!

#7


2  

EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);

#8


1  

TO set the input type of EditText as decimal use this code:

要将EditText的输入类型设置为十进制,请使用以下代码:

EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);

#1


53  

Try using TextView.setRawInputType() it corresponds to the android:inputType attribute.

尝试使用TextView.setRawInputType()它对应于android:inputType属性。

#2


147  

There's no reason to use setRawInputType(), just use setInputType(). However, you have to combine the class and flags with the OR operator:

没有理由使用setRawInputType(),只需使用setInputType()。但是,您必须将类和标志与OR运算符组合在一起:

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

#3


12  

The best way to do that programmatically is using the next method:

以编程方式执行此操作的最佳方法是使用下一个方法:

public static DigitsKeyListener getInstance (boolean sign, boolean decimal) 

Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

返回一个DigitsKeyListener,它接受数字0到9,加上减号(仅在开头)和/或小数点(每个字段只有一个),如果指定的话。

This solve the problem about the many '.' in EditText

这解决了很多'''的问题。在EditText中

editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers. 

editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers. 

editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.

editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.

#4


9  

put this line in xml

把这行放在xml中

 android:inputType="number|numberDecimal"

#5


5  

Use this. Works fine

用这个。工作正常

input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

#6


4  

Hi All I also had this problem. I use C# with Xamarin

大家好我也有这个问题。我使用C#和Xamarin

Just a slight note that I hope someone else as well.

只是略微注意,我希望其他人也是。

I have tried multiple of the methods mentioned here.

我尝试了这里提到的多种方法。

edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);

was the closest but still did not work. As Ralf mentioned, you could use

是最接近但仍然没有奏效。正如拉尔夫所说,你可以使用

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

However in my C# I did not have this. instead you do it as follow:

但是在我的C#中我没有这个。相反,你这样做如下:

edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;

Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!

请注意,这些的顺序很重要。如果你首先放置十进制,它仍然允许你键入任何字符,其中如果Numer是第一个它只是数字,但允许小数分隔符!

#7


2  

EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);

#8


1  

TO set the input type of EditText as decimal use this code:

要将EditText的输入类型设置为十进制,请使用以下代码:

EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);