如何在EditText中为特定文本加下划线?

时间:2013-02-12 10:06:25

标签: android android-layout android-edittext

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="10dp"
        android:text="Don&apos;t worry we will never share this with anyone\nBy Clicking register you are indicating that you have read and agreed to the \n **Terms of services**  and **Privacy Policy**"
        android:textColor="@android:color/black" 
        android:textSize="8.5sp"
    />

根据上述代码,我需要强调服务条款和隐私政策,这些条款以粗体显示。

我曾尝试使用<u></u>,但没有效果。

此外,我还需要增加服务条款,隐私政策

的字体

请提出一些建议。

2 个答案:

答案 0 :(得分:1)

您需要在strings.xml文件中定义文本。是的,您也可以在其中定义格式化的html。

所以你可以尝试一下:

<string name="nice_html">
<![CDATA[Don&apos;t worry we will never share this with anyone<br/>
By Clicking register you are indicating that you have read and agreed to the <br/>
 <u>Terms of services</u>  and <u>Privacy Policy</u>
]]></string>

然后,在您的代码中:

TextView foo = (TextView)findViewById(R.id.foo); foo.setText(Html.fromHtml(getString(R.string.nice_html)));

Original answer

这是最干净的方法.Inshallah。

答案 1 :(得分:0)

String mString=new String("Don&apos;t worry we will never share this with anyone\nBy Clicking register you are indicating that you have read and agreed to the \n **Terms of services**  and **Privacy Policy**");
SpannableString span = new SpannableString(mString);
span.setSpan(new UnderlineSpan(), index of T in terms, mString.length(), 0);
mTextView.setText(span);