Android Edittext type number make appear keyboard with numbers and comma

Asked

Viewed 71 times

1

I am working with values in editText and I need the input to be only numbers and comma to floating point. I tried with inputType="numberDecimal" but when opening the keyboard only allows the point and not the comma. I need the input in the Brazilian standard... ex: 1.00

<EditText
android:id="@+id/tf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:digits="0123456789,"
android:inputType="numberDecimal"
 />

Someone’s been through it?

  • 1

    You would be able to present the code in question here @programadorCS ?

  • opa, sorry, I had put but for some reason did not appear in the question, I put in the answer

  • Edit your question and add code please.

  • Here’s a solution https://www.youtube.com/watch?v=ydF4sQr8kAs

  • Murillo Comino, it didn’t work

  • What I usually do is leave the input with the "." itself, but I change the textwatcher to when to use the "." switch to ",".

  • Murillo Comino is giving replace on onTextChanged but this locking component seems to loop when type

Show 2 more comments

2 answers

0

I got it, guys, there’s been a mix-up of different slides keyboard, android bug. instead of making a new keyboard I called the default 12 digits to always be the same, and limited digits and comma in Edittext xml

xml:

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

java:

editText.setRawInputType(Configuration.KEYBOARD_12KEY);

0

I was able to create a textWatcher for the component:

 TextWatcher() {
        private String current = "";
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.toString().equals(current)) {
                String clean = s.toString();
                if(current.contains(",") && clean.contains(".") ){
                    clean = clean.replace(".", "");
                }
                clean = clean.replace(".", ",");
                current = clean;
                editText.setText(current);
                editText.setSelection(current.length());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

The point is that now some devices on the keyboard the ','(comma) button turns into '.' (dot), need to be the keyboard with comma only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.