本文實(shí)例為大家分享了TextView部分文字可點(diǎn)擊跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供石門網(wǎng)站建設(shè)、石門做網(wǎng)站、石門網(wǎng)站設(shè)計(jì)、石門網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、石門企業(yè)網(wǎng)站模板建站服務(wù),十年石門做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
效果圖:
需求:每個(gè)item的文字都有兩部分是連接可點(diǎn)擊
當(dāng)然需要用到SpannableString和ClickableSpan。
import android.text.TextPaint; import android.text.style.ClickableSpan; import android.view.View; import com.xuehu365.xuehu.App; import com.xuehu365.xuehu.R; /** * Created by Administrator on 2017/10/11. */ public class RecordClickSpan extends ClickableSpan { @Override public void onClick(View widget) { } @Override public void updateDrawState(TextPaint ds) { ds.setColor(App.getApplication().getResources().getColor(R.color.c0066CC)); ds.setUnderlineText(false); } }
private void getRecordContent(BaseViewHolder holder, final StudyLogModel logModel) { TextView record = holder.getView(R.id.record); String courseName = !TextUtils.isEmpty(logModel.getCourseTitle()) ? "《" + logModel.getCourseTitle() + "》" : ""; String lessonName = ":《" + (!TextUtils.isEmpty(logModel.getSourceTitle()) ? logModel.getSourceTitle() + "》" : logModel.getLessonTitle() + "》"); SpannableString courseSpannable = new SpannableString(courseName); SpannableString lessonSpannable = new SpannableString(lessonName); RecordClickSpan courseSpan = new RecordClickSpan() { @Override public void onClick(View widget) { URLBus.doURL("http://www.baidu.com", "", context); } }; RecordClickSpan lessonSpan = new RecordClickSpan() { @Override public void onClick(View widget) { URLBus.doURL("http://www.taobao.com", "", context); } }; courseSpannable.setSpan(courseSpan, 0, courseName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); lessonSpannable.setSpan(lessonSpan, 0, lessonName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); switch (Integer.parseInt(logModel.getSourceType())) { case CoursePlanAdapter.VIDEO_LIVE: case CoursePlanAdapter.VIDEO_RECODE: record.append("觀看"); record.append(courseSpannable); record.append("的視頻"); record.append(lessonSpannable); break; case CoursePlanAdapter.VOICE_LIVE: case CoursePlanAdapter.VOICE_RECODE: record.append("收聽"); record.append(courseSpannable); record.append("的語(yǔ)音"); record.append(lessonSpannable); break; case CoursePlanAdapter.TEACH_FACE: record.append("在"); record.append(courseSpannable); record.append("的面授課時(shí)"); record.append(lessonSpannable); record.append("簽到成功"); break; case CoursePlanAdapter.SURVEY: record.append("完成"); record.append(courseSpannable); record.append("的調(diào)研"); record.append(lessonSpannable); break; case CoursePlanAdapter.EXAM: record.append("完成"); record.append(courseSpannable); record.append("的考試"); record.append(lessonSpannable); break; case CoursePlanAdapter.WORK: record.append("完成"); record.append(courseSpannable); record.append("的作業(yè)"); record.append(lessonSpannable); break; case CoursePlanAdapter.ARTICLE: record.append("閱讀"); record.append(courseSpannable); record.append("的文章"); record.append(lessonSpannable); break; case CoursePlanAdapter.FILE: record.append("下載"); record.append(courseSpannable); record.append("的文檔"); record.append(lessonSpannable); break; default: record.append("完成"); record.append(courseSpannable); record.append("的"); record.append(lessonSpannable); break; } record.setMovementMethod(LinkMovementMethod.getInstance()); }
注意:
1.繼承ClickableSpan用來(lái)改鏈接顏色和去掉下劃線
2.文字的每個(gè)部分的鏈接都要單獨(dú)生成一個(gè)SpannableString和ClickableSpan,然后對(duì)應(yīng)的setSpan
3.特別特別要注意的是!TextView最后append的時(shí)候一定要分別進(jìn)行,不能一起append,這個(gè)坑花了我好些時(shí)間
比如:
-----------錯(cuò)誤 record.append("觀看"+courseSpannable+"的視頻"+lessonSpannable); -----------正確 record.append("觀看"); record.append(courseSpannable); record.append("的視頻"); record.append(lessonSpannable);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。