? ? TextView textView = (TextView) findViewById(R.id.text1);
? ? SpannableStringBuilder spannable = new SpannableStringBuilder("可以點擊的");
? ? //設置文字的前景色,2、4分別表示可以點擊文字的起始和結束位置。
? ? spannable.setSpan(new ForegroundColorSpan(Color.RED),2,4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? //這個一定要記得設置,不然點擊不生效
? ? textView.setMovementMethod(LinkMovementMethod.getInstance());
? ? spannable.setSpan(new TextClick(),2,4 ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
? ? textView.setText(spannable);
}
private class TextClick extends ClickableSpan{
自定義參數 在點擊事件里面進行區(qū)別
@Override
? ? public void onClick(View widget) {
? ? ? ? //在此處理點擊事件
? ? ? ? Log.e("------->", "點擊了");
? ? }
? ? @Override
? ? public void updateDrawState(TextPaint ds) {ds.setColor(ds.linkColor); //文字的顏色 ds.setUnderlineText(true); //是否設置下劃線,true表示設置。 } } }