Domanda

I want to replace the value in an editText if user enter two decimal points by mistake

 else if(txtLdays.getText().toString().trim().contains(".."))
             {


                txtLdays.requestFocus();    
                txtLdays.setError("Double decimal ?");  

             // something like this
               ??   txtLdays.replaceWith(".." , ".");

                 return false;
             }
È stato utile?

Soluzione

Use String#replaceAll() and match the . more than one times in the string.

 txtLdays.setText(txtLdays.getText().toString().replaceAll("([\\.]{2,})", "."));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top