There is an EditText sum entry field with the type "numberDecimal" .

The problem is that after the comma you can put a lot of numbers, but I need no more than 2 (a penny).

How to set the condition that after the comma was not more than 2 characters?

UPD: While there are no imputed answers, he picked himself and did the following:

  etSumm.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { //Переменная результата Boolean res = false; //Считываем что находится в EditText String str = etSumm.getText().toString(); //Узнаем какой символ был нажат char c = keyEvent.getDisplayLabel(); //Преобразуем символ в строку, для сравнения String sc = String.valueOf(c); //Если нажата точка и она не первая в строке - разрешаем поставить if (sc.equals(".") && str.length() > 0 ){ res = false; }//Если точки нет и входящий символ не точка - разрешаем поставить else if (str.indexOf(".") == -1 && !sc.equals(".")) { res = false; }//Если точка не первый символ и после нее не больше 2 символов - разрешаем ввод else if ((str.indexOf(".") > 0) && (str.lastIndexOf(".") >= str.length() - 2)) { res = false; } else {//В остальных случаях - запрещаем ввод res = true; } return res; } }); 

Perhaps there is some more humane method proposed by Google? Very surprised that there is no type of clavus for entering money, or is there? )

2 answers 2

In EditText with the android:inputType="numberDecimal" attribute, I cannot enter a comma, but you can enter a period (apparently it depends on the local decimal separator).

Here's an example with a dot:

 mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { String str = s.toString(); int p = str.indexOf("."); if (p != -1) { String tmpStr = str.substring(p); if (tmpStr.length() == 4) { s.delete(s.length()-1, s.length()); } } } }); 

It uses the TextWatcher interface, which allows you to follow the input to EditText . In the afterTextChanged(...) method, the current content of the EditText checked, if after the next input we get the third character after the point, then this character is removed from the EditText .

  • I agree, your version of the listener is better and more correct, I also added a check that the point was not the first in the line. - Eugene Zaychenko

Based on the answer post_zeew and added to check for a point as the first character in the string

 etSumm.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { //Считываем вводимый текст String str = editable.toString(); //Узнаем позицию int position = str.indexOf("."); //Если точка есть делаем проверки if (position != -1) { //Отрезаем кусок строки начиная с точки и до конца строки String subStr = str.substring(position); //Отрезаем строку с начала и до точки String subStrStart = str.substring(0, position); //Если символов после точки больше чем 3 или если точка первая в строке - удаляем последний if (subStr.length() > 3 || subStrStart.length() == 0) { editable.delete(editable.length() - 1, editable.length()); } } } }); 

It may be useful to someone.