How to make an email address clickable?

前端 未结 5 793
-上瘾入骨i
-上瘾入骨i 2020-12-15 03:10

I have some text in my application that says in case you need extra help, please email us and here is the email address, blah, blah.

But I want them to be able to cl

5条回答
  •  自闭症患者
    2020-12-15 03:37

    The accepted answer may work for emails but if you want to detect different patterns like emails, contact numbers, weblink and set a separate on click implementations for these patterns I suggest you to use CustomClickableEmailPhoneTextview

    Sample Code to use the library.

    CustomPartialyClickableTextview customPartialyClickableTextview= (CustomPartialyClickableTextview) findViewById(R.id.textViewCustom);
    
                    /**
                     * Create Objects For Click Patterns
                     */
                    ClickPattern email=new ClickPattern();
                    ClickPattern phone=new ClickPattern();
                    ClickPattern weblink=new ClickPattern();
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is email
                     */
                    email.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
    
                            Toast.makeText(MainActivity.this,"email clicked",Toast.LENGTH_LONG).show();
    
    
                        }
                    });
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is phone
                     */
                    phone.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
                            Toast.makeText(MainActivity.this,"phone clicked",Toast.LENGTH_LONG).show();
    
                        }
                    });
    
                    /**
                     * set Functionality for what will happen on click of that pattern
                     * In this example pattern is weblink
                     */
                    weblink.setOnClickListener(new ClickPattern.OnClickListener() {
                        @Override
                        public void onClick() {
                            Toast.makeText(MainActivity.this,"website clicked",Toast.LENGTH_LONG).show();
    
                        }
                    });
    
                    /**
                     * set respective regex string to be used to identify patter
                     */
                    email.setRegex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"); // regex for email
                    phone.setRegex("[1-9][0-9]{9,14}"); // regex for phone number
                    weblink.setRegex("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"); // regex for weblink
    
                    /**
                     * add click pattern to the custom textview - first parameter is tag for reference second parameter is ClickPattern object
                     */
                    customPartialyClickableTextview.addClickPattern("email",email);
                    customPartialyClickableTextview.addClickPattern("phone",phone);
                    customPartialyClickableTextview.addClickPattern("weblink",weblink);
    

提交回复
热议问题