allow only number and period(.) in edit text in android

前端 未结 8 959
北海茫月
北海茫月 2020-12-17 09:16

i want to allow only numbers and period(.) in a android edit text. How to do this .. can any body help?

相关标签:
8条回答
  • 2020-12-17 09:37

    Use android:inputType="numberDecimal".

    EDIT: Saw that you later wrote that you want to use this as an editText for IP numbers. Don't use my solution then. For "digits" (not numbers) plus the dot this won't work.

    0 讨论(0)
  • 2020-12-17 09:41

    Use android:numeric property.

    Here an example http://www.androidpeople.com/android-edittext-numeric/

    0 讨论(0)
  • 2020-12-17 09:48

    I found a tentative solution.

    Click on the the EditText in the design view and then find the input property on the far right corner.

    Select Number and textWebEditText. Albeit it won't be the same as the view of the input type set for purely numbers, but it does away with process of having to press the number button at the bottom left of the keyboard every time.

    Plus, unlike the EditText's that have only the number property set and thus the '.' period or decimal symbol is shown but cannot be selected, this input selection allows for the '.' symbol to be selectable.

    Bellow is the XML property:

    android:inputType="number|textWebEditText"
    
    android:numeric="integer"
    

    Setting the numeric property to integer is not necessary to accomplish this, but I just included it as an extra setting to force it. I am only taking in an IP Address in the EditText so I only need numbers. If your solution needs to account for more than strictly integers, then I would not include that line and find another setting, perhaps decimal that would be more appropriate for your data.

    Please note that because this solution makes it work and doesn't necessarily force a format on the input, (ie--> the user can input a '.' yes, but there is no limit to how many time the '.' can be input--> thus this would cause an error when you take the input and use it for Socket or Web Programming. Further input checking and looping may be required before assigning the input to an IP address or something similar. Just putting that out there.

    0 讨论(0)
  • 2020-12-17 09:50

    Currently, specific default input types (numeric, phone number) use KeyListener. You'll just have to implement it to verify your data, and use setKeyListener in your EditText.

    0 讨论(0)
  • 2020-12-17 09:51

    i have set this two lines in the textedit from my your_class.xml

    android:inputType="numberDecimal"
    android:digits="0123456789."
    

    the first line is for setting the keybordart (only numbers and the dot). the second line only allows predetermined characters.

    0 讨论(0)
  • 2020-12-17 09:53

    You need to make your EditText as numberSigned and numberDecimal. Add this line in your xml file -

    android.inputType="numberSigned|numberDecimal"
    

    Done :-)

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