如何设置autoLink =“web”链接?
问题描述:
我有以下的TextView:如何设置autoLink =“web”链接?
<TextView android:id="@+id/theFooBar"
android:autoLink="web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/fooBar"
android:textColor="@android:color/black"
android:textSize="20sp"/>
和字符串:
<string name="fooBar">Foo <u>bar</u>.</string>
这给了我黑色的,带下划线的文本。如果我想为链接添加蓝色,不带下划线的文字(“酒吧”部分),但是我希望其余部分(“foo”部分)是黑色的?我怎么能做到这一点?
答
我建议你在这种情况下使用的WebView而不是TextView的:
WebView web = (WebView) findViewById(R.id.theFooBar);
String str = "<font color='blue'>bar</font><font color='black'><u>foo</u></font>";
web.setBackgroundColor(0);
// It will sets the background color from white to transparent.
web.loadData(str, "text/html", "utf8");
答
添加机器人:textColorLink在TextView中定义链接
这似乎是工作文本颜色,但我要如何处理点击?我调用了setOnClickListener(),但是当我点击WebView时,我的回调从不被调用。 – 2011-05-14 03:54:25
是的,使用WebView可能会起作用,但只是为了让一些文本具有不同的颜色,这太复杂了。特别是在涉及到本地化时。我最终将两个文本视图放在一起。 – 2011-05-14 04:36:25
您可以使用JavaScript来处理您的点击。但是,如果所有问题都出现在这个问题上,那么你一直想为链接(“酒吧”部分)添加“非下划线文本”,但是我希望其余部分(“foo”部分)是黑色的。使用两个文本视图是最佳解决方案。我已经给你提供了更广泛的解决方案,想象你必须做更多的事情而不是必需的事情。 – George 2011-05-14 07:28:26