开发者

With the android editText control is there a simple way to display only numbers?

开发者 https://www.devze.com 2023-01-08 19:05 出处:网络
I have a simple app that allows the user to push a button that brings up the list of contacts.They select the contact and the application then puts the phone number into an edit text control.This all

I have a simple app that allows the user to push a button that brings up the list of contacts. They select the contact and the application then puts the phone number into an edit text control. This all works like a charm.

However, I notice that the phone number that is retrieved has 'punctuation' marks in it: 123.456.7890 or 123-456-7890 is returned verbatim with the dots and dashes included.

Is there a simple way to s开发者_开发问答trip out non-numeric entry before displaying back to the user?


You should be able to manipulate the string using standard Java String functions to remove non numbers.

i.e.

myString.replaceAll("\D", "");

\D is a regex that should match anything that isn't a digit. See Pattern docs.

replaceAll will replace all instances of the regex with whatever you provide as the second param.

0

精彩评论

暂无评论...
验证码 换一张
取 消