2011-12-09 52 views
3

我有按钮和文本,我retrive从即 我宣布样如何从android中的超链接文本中删除下划线?

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

我删除其文本的颜色从蓝色到白色,但在strings.xml中RES /值的按钮文本string.xml我如何删除其下划线? 在我的.java文件我只使用setMovementMethod(LinkMovementMethod.getInstance());作出链接clickabale ...我没有使用Linkify,webview或类似form.Html ... 一切工作正常。我只是想删除下面的“谷歌”文本的下划线... 有没有办法在XML中做到这一点?我也使用android:autoLink="all"。但是当我使用它时,文本和按钮颜色会发生变化,我不希望这样。

请帮忙。

+0

这里有一个很棒的答案在这里:http://stackoverflow.com/questions/4096851/remove-underline-from-links-in-textview-android – Warpzit

+0

是啊之前发布问题我谢谢你的回答,但我在这里已经提到过,我没有使用Linkify或其他任何东西,除了 setMovementMethod(LinkMovementMethod.getInstance()); 但无论如何。 – shankey

+0

感谢您的编辑[email protected] – shankey

回答

1

尝试了很多后,我终于找到了解决方案。 我删除了string.xml 的链接,并在java文件中添加了这段代码。

Button btnGoogle = (Button GoogleAlert.findViewById(
       R.id.btnGoogle); 
      btnGoogle.setMovementMethod(
       LinkMovementMethod.getInstance()); 
      btnGoogle.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       {   
        Uri uri = Uri.parse(
         "http://www.google.com"); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
        startActivity(intent); 
        buyAlert.dismiss(); 
       } 
      }); 

它的工作完美。

+0

完美无缺。谢谢。 – Sahan

1

您可以将style=\"text-decoration:none\"添加到您的<a>标记中。

有关如何格式化字符串资源的更多信息,请查看String Resources Documentation

+0

谢谢!但是我做到了,但它不起作用。下划线仍然保持在文本之下。 – shankey

+2

请发布完整的''标签,并附上我提到的修改。 –

+0

这是我的字符串标记 Google shankey

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

    @Override 
    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);

+0

我这样得到灰色链接 – CQM

+0

p_DrawState.setColor(R.color.info_text_color);使用这个你可以改变超链接文本的颜色...... String value =“ click to go google”;“这里改变颜色为其他颜色 – Sishin

-3
TextView text=(TextView) findViewById(R.id.text); 
final String s = text.getText(); 
text.setText(""); 
text.setText(s); 

这就是所有人))

+0

对不起,不适用于我:TextView creditsTextView =(TextView)dialog.getCustomView()。findViewById(R.id.text_credits); creditsTextView.setMovementMethod(LinkMovementMethod.getInstance()); final CharSequence s = creditsTextView.getText(); creditsTextView.setText(“”); creditsTextView.setText(s); –