如何在TextView上一次将下划线和颜色设置为部分文本?

aco*_*tic 0 html fonts android colors textview

我的EditText中有一个字符串,而strre是此字符串中的URL链接.

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

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

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

有人能帮我吗?谢谢!

pjh*_*ink 5

我在运行Android 2.2的模拟器,运行Android 3.2的另一个模拟器以及运行Android 4.0.3的手机上尝试了这个,并且您发布的代码在所有三个平台上都能正常工作(文本"some link"都带有下划线和蓝色).

这是我使用的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

这是完整的活动代码:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

package com.example.andtest01;

<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>
Run Code Online (Sandbox Code Playgroud)