最近在做评论的时候需要实现这种效果网上百度了一下,就是自定义一个类继承4ClickableSpan,然后在updateDrawState方法中设置是否下划线为false,但是看了一下网上实现的方法是直接在新类的OnClick方法中实现onClick操作,感觉不太容易扩展使用,于是我自定义了一个接口,通过接口实现了外部代码调用,具体代码如下:
public class NoLineClickableSpan extends ClickableSpan{ private IOnNoLineTextClick iOnNoLineTextClick; public NoLineClickableSpan() { super(); } @Override public void updateDrawState(TextPaint ds) { ds.setColor(ds.linkColor); ds.setUnderlineText(false); } @Override public void onClick(View widget) { iOnNoLineTextClick.onClick(); } public void setOnNoLineTextClick(IOnNoLineTextClick i){ iOnNoLineTextClick = i; } public interface IOnNoLineTextClick{ public void onClick(); } }XML文件:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小西回复大东方:你是个大大大的好好人" /> </RelativeLayout>
public class MainActivity extends Activity { private TextView tv_test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tv_test = (TextView) findViewById(R.id.tv_test); tv_test.setMovementMethod(LinkMovementMethod.getInstance()); SpannableString spanString = new SpannableString(tv_test.getText().toString()); NoLineClickableSpan span = new NoLineClickableSpan(); span.setOnNoLineTextClick(new IOnNoLineTextClick() { @Override public void onClick() { System.out.println("点击用户"); } }); spanString.setSpan(span, 0, 2, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); tv_test.setText(spanString); } }
Android自定义无下划线ClickableSapn超链接文本样式
原文地址:http://blog.csdn.net/jav_imba/article/details/46410747