Android active link of url in TextView

前端 未结 10 508
盖世英雄少女心
盖世英雄少女心 2020-11-30 22:24

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like hello

10条回答
  •  [愿得一人]
    2020-11-30 22:43

    There are 2 cases:

    • the text looks like "click on http://www.hello.com"

    then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:

    
    
    • the text looks like click on hello

    then you have to do it by code and tell the text is html, and specify a Link movement method for the click:

        String text = "click on hello";
        TextView textView = view.findViewById(R.id.textView);
        textView.setText(Html.fromHtml(text));
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    

提交回复
热议问题