本文实例为大家分享了TextView部分文字可点击跳转的具体代码,供大家参考,具体内容如下
效果图:
需求:每个item的文字都有两部分是连接可点击
当然需要用到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("的语音"); record.append(lessonSpannable); break; case CoursePlanAdapter.TEACH_FACE: record.append("在"); record.append(courseSpannable); record.append("的面授课时"); record.append(lessonSpannable); record.append("签到成功"); break; case CoursePlanAdapter.SURVEY: record.append("完成"); record.append(courseSpannable); record.append("的调研"); 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("的作业"); 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用来改链接颜色和去掉下划线
2.文字的每个部分的链接都要单独生成一个SpannableString和ClickableSpan,然后对应的setSpan
3.特别特别要注意的是!TextView最后append的时候一定要分别进行,不能一起append,这个坑花了我好些时间
比如:
-----------错误 record.append("观看"+courseSpannable+"的视频"+lessonSpannable); -----------正确 record.append("观看"); record.append(courseSpannable); record.append("的视频"); record.append(lessonSpannable);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。