Android EditText shows both a comma and a period as a possible separator in case of numberDecimal input type

I need to allow the user to choose between two options when he enters a decimal number:

use comma (,) as a separator

use period (.) as a separator

By default, if I use inputType="numberDecimal"in EditTextxml config - EditTextas soon as possible separators and comma digits ( ,).

I tried to use it android:digits="0123456789in my configuration EditText, but with no result - the EditTextwidget shows only numbers and a comma.

I want to have both options ( .and ,) available to the user on the on-screen keyboard when he tries to enter a decimal number.

Could you advise?

+4
4

android:inputType="numberDecimal", android:digits="0123456789,." ( ). , , , .

, . .

+12

. , . , - .

 etlocation = (EditText) findViewById(R.id.etlocation);

 etlocation.getText().toString();

 if (!isValidLocation(etlocation.getText().toString().trim()))
 {
 etlocation.setError("Invalid location");
 }

 public static boolean isValidLocation(String str) {
    boolean isValid = false;
    String expression = "^[0-9,.]*$";
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
+1
0

Source: https://habr.com/ru/post/1537093/


All Articles