Android: Limiting EditText to numbers

后端 未结 5 405
别跟我提以往
别跟我提以往 2020-12-20 12:39

When creating an EditText in the java portion of the application, how do you limit it to numbers like you would in the xml? For example:

new EditText(this);         


        
相关标签:
5条回答
  • 2020-12-20 12:52

    this will usually do it:

    android:numeric="integer"

    0 讨论(0)
  • Simply use the below lines in the xml file

    To accept only Numbers put:

     android:inputType="number"
    

    To limit the length of the input numbers:

    android:maxLength="10"
    

    To accept only specific numbers:

    EX:1 The below line accept only 1 or 0.

    android:digits="10"
    

    EX:2 The below line accept only 2 or 3.

    android:digits="23"
    
    0 讨论(0)
  • 2020-12-20 13:02

    Something like this perhaps ?

    EditText text = new EditText(this);
    text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
    
    0 讨论(0)
  • 2020-12-20 13:04

    Use of android:numeric is deprecated, use android:inputType="number"

    0 讨论(0)
  • 2020-12-20 13:15

    Go to the docs and look at the table with XML attributes. It shows you the code equivalent of each item.

    In this case, it's setRawInputType.

    0 讨论(0)
提交回复
热议问题