TextView添加链接
2014-01-02 15:21
211 查看
TextView添加链接
本文主要介绍TextView添加链接的几种可行及不可行方式,并且分析为什么不可行。示例APK地址TrineaAndroidDemo.apk,效果图如下:一、可行方式
Java
12345678 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" />trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo);trineaInfoTv.setMovementMethod(LinkMovementMethod.getInstance());Spanned text = Html.fromHtml("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>");trineaInfoTv.setText(text); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" /> trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo); Spanned text = Html.fromHtml("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>"); trineaInfoTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Uri web = Uri.parse("http://www.trinea.cn"); Intent i = new Intent(Intent.ACTION_VIEW, web); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); activity.startActivity(i); } }); |
Java
1234567 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="all" />trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo);trineaInfoTv.setText("个人主页:http://www.trinea.cn"); |
1 | trineaInfoTv.setText("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>"); |
Java
12345678 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="all" />trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo);Spanned text = Html.fromHtml("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>");trineaInfoTv.setText(text); |
1 2 3 4 5 6 7 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" /> trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo); Spanned text = Html.fromHtml("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>"); trineaInfoTv.setText(text); |
Java
123456789 | <TextView android:id="@+id/trineaInfo" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="all" />trineaInfoTv = (Button)activity.findViewById(R.id.trineaInfo);trineaInfoTv.setMovementMethod(LinkMovementMethod.getInstance());Spanned text = Html.fromHtml("个人主页:<a href=\"http://www.trinea.cn\"> Trinea</a>");trineaInfoTv.setText(text); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | if (!isSuggestionUnderLineRefreshFlag) { if (type == BufferType.EDITABLE || mInput != null || needEditableForNotification) { Editable t = mEditableFactory.newEditable(text); text = t; setFilters(t, mFilters); InputMethodManager imm = InputMethodManager.peekInstance(); if (imm != null) imm.restartInput(this); } else if (type == BufferType.SPANNABLE || mMovement != null) { text = mSpannableFactory.newSpannable(text); } else if (!(text instanceof CharWrapper)) { text = TextUtils.stringOrSpannedString(text); } } if (mAutoLinkMask != 0) { Spannable s2; if (type == BufferType.EDITABLE || text instanceof Spannable) { s2 = (Spannable) text; } else { s2 = mSpannableFactory.newSpannable(text); } if (Linkify.addLinks(s2, mAutoLinkMask)) { text = s2; type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE; /* * We must go ahead and set the text before changing the * movement method, because setMovementMethod() may call * setText() again to try to upgrade the buffer type. */ mText = text; // Do not change the movement method for text that support text selection as it // would prevent an arbitrary cursor displacement. if (mLinksClickable && !textCanBeSelected()) { setMovementMethod(LinkMovementMethod.getInstance()); } } } |
相关文章推荐
- GObject对象系统
- junit 单元测试,测试上传相片
- java 读取excel 2003版
- sicily. 1815 计算两点间的距离
- 过滤敏感词,脏词
- KVM虚拟机的创建、管理与迁移
- C# DataGridView 添加Button -转
- PODOFO编译之3——可用篇
- opencv小波变换代码
- jquery实现checkbox全选全不选的简单实例
- 计算机常用缩略语汇编
- django test模块
- jquery API .hasClass()
- Android深入浅出之Binder机制
- php中有用插件集合
- 【Linux】基于Linux的buffer和cache学习
- RHEL6基础四十九之RHEL文件(夹)权限进阶篇
- 利用solr搭建企业级应用搜索
- NDK的环境搭建和使用
- 新东西的学习