문제

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