dev*_*oid 55 url android textview
我从Web服务获取动态文本并在TextView中显示相同的内容.有时TextView有url之类的<a href="http://hello.com">hello</a>.我使用以下代码设置了文本.
textView.setText(Html.fromHtml(sampletext));
Run Code Online (Sandbox Code Playgroud)
并且还在android:autoLink="web"相应的xml中设置了包含的内容TextView.现在链接显示正确的蓝色和下划线,但我发现它只是一个死链接.如果我们试图点击它,什么也没发生.我需要做些什么才能使链接生效?
L. *_* G. 144
重新审视所有解决方案后,总结一些解释:
android:autoLink="web"
Run Code Online (Sandbox Code Playgroud)
即使未设置android:linksClickable,也会找到一个URL并创建一个链接,默认情况下链接是可点击的.您不必单独保留URL,即使在文本中间,它也会被检测到并可单击.
<TextView
android:text="My web site: www.stackoverflow.com"
android:id="@+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:autoLink="web">
</TextView>
Run Code Online (Sandbox Code Playgroud)
要通过代码设置链接,原理相同,不需要模式或android:布局中的自动链接,使用Linkify自动找到链接:
final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
myClickableUrl.setText("Click my web site: www.stackoverflow.com");
Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
Run Code Online (Sandbox Code Playgroud)
Vin*_*tti 36
这对我有用:
<TextView
android:text="www.hello.com"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web">
</TextView>
Run Code Online (Sandbox Code Playgroud)
M.A*_*yed 14
要保存任何一次真正的解决方案
<TextView
android:text="www.hello.com"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:autoLink="web"
android:linksClickable="true">
</TextView>
Run Code Online (Sandbox Code Playgroud)
我使用它,它的工作完美
有2种情况:
"click on http://www.hello.com"然后你只需要在xml中设置autoLink属性,以便在文本中自动检测到链接:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="click on http://www.hello.com"/>
Run Code Online (Sandbox Code Playgroud)
click on <a href="http://hello.com">hello</a>那么你必须通过代码来完成并告诉文本是html,并为点击指定链接移动方法:
String text = "click on <a href=\"http://hello.com\">hello</a>";
TextView textView = view.findViewById(R.id.textView);
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
看看这种方法:
String text = "Visit stackoverflow.com";
TextView label = new TextView(this);
label.setText(text);
Pattern pattern = Pattern.compile("stackoverflow.com");
Linkify.addLinks(label, pattern, "http://");
Run Code Online (Sandbox Code Playgroud)