1.新建ComponentClickableSpan类重写ClickableSpan
public class ComponentClickableSpan extends ClickableSpan {
private Context context;
public ComponentClickableSpan(Context context){
this.context = context;
}
@Override
public void onClick(@NonNull @android.support.annotation.NonNull View widget) {
Log.e("info",((TextView)widget).getText().toString());
Intent intent = new Intent(Intent.ACTION_DIAL);//跳转到系统拨号界面
Uri data = Uri.parse("tel:" +((TextView)widget).getText().toString());
intent.setData(data);
context.startActivity(intent);
}
@Override
public void updateDrawState(@NonNull @android.support.annotation.NonNull TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.parseColor("#888888"));//设置字体颜色,
ds.setUnderlineText(false);//设置不显示下划线
}
}
2.实际运用
<TextView
android:id="@+id/personnel_tv_item_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center"
android:text="13900000001"
android:textSize="16sp"/>
TextView textView = holder.getView(R.id.personnel_tv_item_number);
SpannableString spannableString = new SpannableString(data.getNumber());
spannableString.setSpan(new ComponentClickableSpan(PersonnelActivity.this),0,data.getNumber().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());//不加这句没有单击事件