How to click or tap on a TextView text

后端 未结 8 1692
無奈伤痛
無奈伤痛 2020-11-27 09:52

I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.

I keep thinking about bu

8条回答
  •  日久生厌
    2020-11-27 10:10

    To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).

    Linkify

    Use a string resource like:

    Here is a link: http://www.stackoverflow.com
    

    Then in a textview:

    TextView textView = ...
    textView.setText(R.string.links);
    Linkify.addLinks(textView, Linkify.ALL);
    

    Html

    Using Html.fromHtml:

    Here you can put html <a href="http://www.stackoverflow.com">Link!</>
    

    Then in your textview:

    textView.setText(Html.fromHtml(getString(R.string.html)));
    

提交回复
热议问题