How to make links in a TextView clickable?

后端 未结 30 3339

I have the following TextView defined:



        
相关标签:
30条回答
  • 2020-11-21 23:54

    This is how I solved clickable and Visible links in a TextView (by code)

    private void setAsLink(TextView view, String url){
            Pattern pattern = Pattern.compile(url);
            Linkify.addLinks(view, pattern, "http://");
            view.setText(Html.fromHtml("<a href='http://"+url+"'>http://"+url+"</a>"));
        }
    
    0 讨论(0)
  • 2020-11-21 23:54

    Add CDATA to your string resource

    Strings.xml

    <string name="txtCredits"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>
    
    0 讨论(0)
  • 2020-11-21 23:55

    Buried in the API demos I found the solution to my problem:

    Link.java:

        // text2 has links specified by putting <a> tags in the string
        // resource.  By default these links will appear but not
        // respond to user input.  To make them active, you need to
        // call setMovementMethod() on the TextView object.
    
        TextView t2 = (TextView) findViewById(R.id.text2);
        t2.setMovementMethod(LinkMovementMethod.getInstance());
    

    I removed most of the attributes on my TextView to match what was in the demo.

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txtCredits"/>
    

    That solved it. Pretty difficult to uncover and fix.

    Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

    0 讨论(0)
  • 2020-11-21 23:56

    I noticed that using android:autoLink="web" thus

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:autoLink="web"/>
    

    worked OK for URLs but since I had an e-mail address and phone number that I wanted to link as well, I ended up using this line android:autoLink="all" like this

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:autoLink="all"/>
    

    and it worked like a charm.

    0 讨论(0)
  • 2020-11-21 23:56

    Use this...

    TextView.setOnClickListener(new OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com/"));
                            startActivity(in);
                        }
    
                    });
    

    and add permission in manifest file

    <uses-permission android:name="android.permission.INTERNET"/>
    
    0 讨论(0)
  • 2020-11-21 23:57

    I use the autolink to "auto underline" the text, but just made an "onClick" that manages it. (I ran into this problem myself)

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:textSize="18dp"
                android:autoLink="all"
                android:text="@string/twitter"
                android:onClick="twitter"/>
    
    public void twitter (View view)
        {
            try
            {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/onaclovtech"));
                startActivity(browserIntent);
    
            }
            finally
            {
            }
        }
    

    Doesn't require any permissions, as you are passing the intent off to apps that manage those resources, (I.E. browser).

    This was what worked for me. Good luck.

    0 讨论(0)
提交回复
热议问题