Linkify Phone-numbers using Regex

两盒软妹~` 提交于 2019-12-24 07:46:47

问题


I am trying to linkify phonenumbers using regular expressions, but i cant manage to apply it on my setText();

I've googled alot and it feels like im really close to success.

The code i got :

if(tag.equals("Customer")) {     
                     String name = xpp.getAttributeValue(null, separated_nodes[0].trim());
                     String number = xpp.getAttributeValue(null, separated_nodes[1].trim());
                     String SSNumber = xpp.getAttributeValue(null, separated_nodes[2].trim());
                     String Address = xpp.getAttributeValue(null, separated_nodes[3].trim());
                     String Postcode = xpp.getAttributeValue(null, separated_nodes[4].trim());
                     String City = xpp.getAttributeValue(null, separated_nodes[5].trim());
                     String Phone = "Phone#: " + xpp.getAttributeValue(null, separated_nodes[6].trim());
                     String Cell = xpp.getAttributeValue(null, separated_nodes[7].trim());
                     String Email = xpp.getAttributeValue(null, separated_nodes[8].trim());
                    // text.setText("Network "+xpp.getAttributeValue(null, "Name"));
                     Pattern pattern = Pattern.compile("[0]{1}[0-9]{6,15}");
                     Linkify.addLinks(text, pattern, "Phone#: ");
                     //Linkify.addLinks(text, pattern, xmlstring);
                     //Linkify.addLinks(text, pattern, Phone);

                            text.setText("Customer: \nName: " + name +"\n" +
                                    "Customer Number: "+ number + "\n" +
                                    "Social Security Number: "+ SSNumber +"\n" +
                                    "Address: "+ Address +"\n" +
                                    "Postal Code: "+ Postcode +"\n" +
                                    "City: "+ City +"\n" +
                                    ""+ Phone +"\n" +
                                    "Cellphone#: "+ Cell +"\n" +
                                    "e-mail: "+ Email +"\n");



                            Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES) ;


                 } 

As you see, ive tried multiple ways to linkify Phone and Cellphone-number.

I think the RegEx is correct.


回答1:


  1. you need to call Linkify.addLinks() AFTER you have set up your text view, you're doing it before
  2. Doesn't Linkify already support phone numbers, e.g. Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS)?

Update

Also, in here: Linkify.addLinks(text, pattern, "Phone#: "); the third argument is supposed to be the Scheme, "Phone#:" is NOT a valid scheme. It should be tel:.




回答2:


Unfortunately, the call to

Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES)

on the last line is wiping out the Spannables that previous calls to Linkify.addLinks() created. See the Linkify Documentation



来源:https://stackoverflow.com/questions/12936823/linkify-phone-numbers-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!