How do I restrict my EditText input to numerical (possibly decimal and signed) input?

前端 未结 11 1242
旧巷少年郎
旧巷少年郎 2020-12-01 05:20

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.

<
相关标签:
11条回答
  • 2020-12-01 05:23

    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"));
    

    EDIT

    kotlin version

    fun EditText.onlyNumbers() {
        inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
            InputType.TYPE_NUMBER_FLAG_SIGNED
        keyListener = DigitsKeyListener.getInstance("0123456789")
    }
    
    0 讨论(0)
  • 2020-12-01 05:24

    Try having this in your xml of Edit Text:

    android:inputType="numberDecimal"
    
    0 讨论(0)
  • 2020-12-01 05:25

    use setRawInputType and setKeyListener

    editTextNumberPicker.setRawInputType(InputType.TYPE_CLASS_NUMBER | 
                       InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED );
    editTextNumberPicker.setKeyListener(DigitsKeyListener.getInstance(false,true));//set decimals and positive numbers. 
    
    0 讨论(0)
  • 2020-12-01 05:26

    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.

    This solve the problem about the many '.' in 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.
    
    0 讨论(0)
  • 2020-12-01 05:32

    Hi All I also had this problem. I use C# with 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);

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

    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!

    0 讨论(0)
  • 2020-12-01 05:36
    EditText edit = new EditText(this);
    
    edit.setHorizontallyScrolling(true);
    edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
    
    0 讨论(0)
提交回复
热议问题