3

私は 1 時間以上かけて多くの例を調べましたが、TextView にテキストを設定して Web URL にリンクするために実際に機能するものはありません。

サンプルコード!

text8 = (TextView) findViewById(R.id.textView4);
text8.setMovementMethod(LinkMovementMethod.getInstance());

Strings.xml

 <string name="urltext"><a href="http://www.google.com">Google</a></string>

main.xml

 <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:linksClickable="true"
        android:text="@string/urltext"
        android:textAppearance="?android:attr/textAppearanceMedium" />

現在、このコードはテキストを「Google」として表示しますが、ハイパーリンクされておらず、クリックしても何も起こりません。

4

3 に答える 3

1
TextView text=(TextView) findViewById(R.id.text);   

    String value = "<html> click to go <font color=#757b86><b><a href=\"http://www.google.com\">google</a></b></font> </html>";
Spannable spannedText = (Spannable)
                Html.fromHtml(value);
text.setMovementMethod(LinkMovementMethod.getInstance());

Spannable processedText = removeUnderlines(spannedText);
        text.setText(processedText);

これがあなたの removeUnderlines() です

public static Spannable removeUnderlines(Spannable p_Text) {  
               URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);  
               for (URLSpan span : spans) {  
                    int start = p_Text.getSpanStart(span);  
                    int end = p_Text.getSpanEnd(span);  
                    p_Text.removeSpan(span);  
                    span = new URLSpanNoUnderline(span.getURL());  
                    p_Text.setSpan(span, start, end, 0);  
               }  
               return p_Text;  
          }  

クラスURLSpanNoUnderline.javaも作成します

import co.questapp.quest.R;
import android.text.TextPaint;
import android.text.style.URLSpan;

public class URLSpanNoUnderline extends URLSpan {
    public URLSpanNoUnderline(String p_Url) {
        super(p_Url);
    }

    public void updateDrawState(TextPaint p_DrawState) {
        super.updateDrawState(p_DrawState);
        p_DrawState.setUnderlineText(false);
        p_DrawState.setColor(R.color.info_text_color);
    }
}

この行を使用して、そのテキストの色を変更することもできます p_DrawState.setColor(R.color.info_text_color);

于 2014-04-17T21:35:45.020 に答える
-1

CDATA を文字列リソースに追加します

Strings.xml

<string name="urltext"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>
于 2015-02-17T11:19:54.110 に答える