How to make links in a TextView clickable?

后端 未结 30 3308

I have the following TextView defined:



        
30条回答
  •  时光说笑
    2020-11-21 23:51

    Don't know if it's worth adding another answer, but just in case...

    I had to hunt this down in a couple places but finally got this version of the code to work.

    strings.xml:

    <a href="http://www.google.com">link text1</a>
    <a href="http://www.google.com">link text2</a>
    

    myactivity.xml:

    
    
    
    

    myactivty.java (in onCreate()):

    TextView tv1 = (TextView)findViewById(R.id.textview1);
    TextView tv2 = (TextView)findViewById(R.id.textview2);
    
    tv1.setText(Html.fromHtml(getResources().getString(R.string.name1)));
    tv2.setText(Html.fromHtml(getResources().getString(R.string.name2)));
    tv1.setMovementMethod(LinkMovementMethod.getInstance());
    tv2.setMovementMethod(LinkMovementMethod.getInstance());
    

    This will create two clickable hyperlinks with the text link text1 and link text2 which redirect the user to google.

提交回复
热议问题