【问题标题】:how to set underline and color to partly text at one time on TextView?如何在TextView上一次将下划线和颜色设置为部分文本?
【发布时间】:2012-08-30 08:09:14
【问题描述】:

我的 EditText 中有一个字符串,这个字符串中有一个 URL 链接。

所以我想设置这个链接有下划线和蓝色作为常识。

现在我可以使用“u”标签和 Html.fromHtml() 添加下划线,但不能设置颜色,这是我的代码:

String text = "some string <u><font color=\"#0000FF\">some link</font></u>";
editText.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

有人可以帮助我吗?谢谢!

【问题讨论】:

标签: android html fonts colors textview


【解决方案1】:

我在运行 Android 2.2 的模拟器、运行 Android 3.2 的另一个模拟器和运行 Android 4.0.3 的手机上尝试了这个,您发布的代码在所有三个平台上都可以正常工作(文本“一些链接”都带有下划线和蓝色)。

这是我使用的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:hint="@string/hello_world" >

    <requestFocus />
</EditText>

这是完整的活动代码:

package com.example.andtest01;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText1);

        String text = "some string <u><font color=\"#0000FF\">some link</font></u>";
        editText.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
    }

}

【讨论】:

  • 感谢您的调试和反馈,我再次尝试了我的代码,它现在可以工作了!实际上原因是我将它实现到一个新的活动中,但我忘记了这一点并检查了旧活动中的结果。不管我解释清楚,代码确实有效!我会接受你的回答,谢谢!
猜你喜欢
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2012-09-13
相关资源
最近更新 更多