how to set input type to be numberdecimal but also allow a “-”

后端 未结 5 1866
孤城傲影
孤城傲影 2021-02-13 15:26

I have set input type to be numberdecimal but also want to populate the editText with a \"-\" programmatically. I can add the text but then I am unable to edit the text as it do

相关标签:
5条回答
  • 2021-02-13 15:55

    You will have to write your own KeyListener. You could start by downloading the source of the NumberKeyListener and take it from there.

    Martin

    0 讨论(0)
  • 2021-02-13 16:01

    I managed to do that with:

    android:inputType="number|numberSigned"    
    android:digits="0123456789-"
    
    0 讨论(0)
  • 2021-02-13 16:01

    I am having one solution, which may helps you:

    Suppose, You want to enter 2-3 numbers with "-" sign for e.g. 203-304-405.23-232.45,

    then Allow user to enter this in EditText without setting any attributes. and then you can Separate each numbers with "split()" function , but be sure that there should be any separator sign in between the tokens.

    then

     String tokens[];
     strInput = edittext1.getText.toString();
     tokens = strInput.split(",");
    

    then you can work with each tokens separately as tokens[0], tokens[1], for example:

    num1 = tokens[0];
    num2 = tokens[1];
    

    Hope this helps you.

    Enjoy!!

    0 讨论(0)
  • 2021-02-13 16:08

    I found a very easy solution:

    editText.setKeyListener(new DigitsKeyListener(true, true));    
    

    The first true is for whether is signed input, the second true is for decimal.
    Hope that helps

    0 讨论(0)
  • 2021-02-13 16:10

    I was able to achieve this behavior by setting digits xml attribute as follows:

    <EditText
        ...
        android:inputType="number"
        android:digits="0123456789-"/>
    

    Setting it up programatically (Set EditText Digits Programmatically):

    weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789-"));
    
    0 讨论(0)
提交回复
热议问题