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;
             }
有帮助吗?

解决方案

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

 txtLdays.setText(txtLdays.getText().toString().replaceAll("([\\.]{2,})", "."));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top