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

前端 未结 11 1243
旧巷少年郎
旧巷少年郎 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:39

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

    EditText edit = new EditText(this);
    edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
    
    0 讨论(0)
  • 2020-12-01 05:44

    put this line in xml

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

    my solution:`

       public void onTextChanged(CharSequence s, int start, int before, int count) {
       char ch=s.charAt(start + count - 1);
       if (Character.isLetter(ch)) {
           s=s.subSequence(start, count-1);
           edittext.setText(s);
        }
    
    0 讨论(0)
  • 2020-12-01 05:46

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

    0 讨论(0)
  • 2020-12-01 05:47

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

    edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
    
    0 讨论(0)
提交回复
热议问题