Android中TextView实现部分文字可点击跳转

Velika ·
更新时间:2024-11-14
· 500 次阅读

本文实例为大家分享了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); 您可能感兴趣的文章:Android中TextView实现超过固定行数显示“...展开全部”Android中TextView实现分段显示不同颜色的字符串Android实现可点击展开的TextViewAndroid自定义可点击的ImageSpan并在TextView中内置Viewandroid使用Textview实现伸缩效果Android实现可以展开的TextViewAndroid TextView实现词组高亮的示例代码Android中实现词组高亮TextView方法示例Android Textview实现颜色渐变滚动效果Android开发自定义TextView省略号样式的方法Android中TextView局部变色功能实现



点击跳转 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号