- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否可以在使用 Linkify 创建链接后更改 TextView 文本?我有一些东西,我希望 url 有两个字段,一个名称和一个 id,但我只希望文本显示名称。
所以我从一个包含名称和 ID 的文本的 TextView 开始,然后链接化以创建与这两个字段的适当链接。但是为了显示,我不想显示id。
这可能吗?
最佳答案
这有点痛苦,但是是的。所以 Linkify 基本上做了一些事情。首先,它扫描 textview 的内容以查找与 url 相匹配的字符串。接下来它为匹配它的那些部分创建 UrlSpan 和 ForegroundColorSpan。然后它设置 TextView 的 MovementMethod。
这里的重要部分是 UrlSpan 的。如果您使用 TextView 并调用 getText(),请注意它返回一个 CharSequence。这很可能是某种跨越。您可以从 Spanned 中询问 getSpans(),特别是 UrlSpans。一旦您知道了所有这些跨度,您就可以遍历列表并查找旧跨度对象并将其替换为新跨度对象。
mTextView.setText(someString, TextView.BufferType.SPANNABLE);
if(Linkify.addLinks(mTextView, Linkify.ALL)) {
//You can use a SpannableStringBuilder here if you are going to
// manipulate the displayable text too. However if not this should be fine.
Spannable spannable = (Spannable) mTextView.getText();
// Now we go through all the urls that were setup and recreate them with
// with the custom data on the url.
URLSpan[] spans = spannable.getSpans(0, spannable.length, URLSpan.class);
for (URLSpan span : spans) {
// If you do manipulate the displayable text, like by removing the id
// from it or what not, be sure to keep track of the start and ends
// because they will obviously change.
// In which case you may have to update the ForegroundColorSpan's as well
// depending on the flags used
int start = spannable.getSpanStart(span);
int end = spannable.getSpanEnd(span);
int flags = spannable.getSpanFlags(span);
spannable.removeSpan(span);
// Create your new real url with the parameter you want on it.
URLSpan myUrlSpan = new URLSpan(Uri.parse(span.getUrl).addQueryParam("foo", "bar");
spannable.setSpan(myUrlSpan, start, end, flags);
}
mTextView.setText(spannable);
}
希望这是有道理的。 Linkify 只是一个设置正确 Span 的好工具。呈现文本时,跨度只会被解释。
关于android - 我可以在使用 Linkify 后更改 TextView 链接文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3983409/
我正在为我的基于 Espresso 的 Android 应用程序编写一些测试。单击 TextView 内的链接(使用 Linkify 类创建)后,我需要断言我看到的是正确的屏幕。 我尝试单击包含链接的
我正在使用 Linkify 检测 TextView 中的标签 (#)。 Pattern userMatcher = Pattern.compile("\\B@[^:\\s]+"); String us
我有一个 TextView,其中包含一些具有 URL 的文本。我已经使用 Linkify 将它们变成可点击的 URL: Linkify.addLinks(bodyTextView, Linki
有什么方法可以设置由 Linkify.addLinks() 操作生成的链接的样式吗?具体来说,我想更改默认的蓝色。 谢谢 最佳答案 我认为样式 linkify 是可能的,但如果你想添加链接,你可能想使
我正在使用带有自定义 TransformFilter 的 Linkify。是否可以告诉 linkify 只匹配我的模式的第一次出现? 最佳答案 我会为此使用 MatchFilter,就像这样 M
我正在尝试将 Linkify 与自定义模式结合使用,该模式应该匹配长度为 10 或 11 位的电话号码。我写了以下代码 - Pattern japPhoneNoPattern = Pattern.co
我想添加一个到 TextView 的链接并具有以下代码: TextView textview = (TextView) findViewById(R.id.mytext); Pattern myPat
我正在处理我的项目,我有一个 listView,每个项目都是一个 LinaerLayout,它有一个带有 Linkify 超链接的 TextView。所以,当我在 ListView 中按下一个项目时,
我在自定义适配器中使用的 linkify 似乎遇到了问题。出于某种原因,当我点击其中一个链接时,我收到了以下堆栈信息: 06-07 20:49:34.696: ERROR/AndroidRuntime
使用后 Linkify.addLinks(content, pattern_glos, scheme_glos, null, glosFilter); 日志写入 11-22 21:19:15.319:
例如,当您这样做时, Linkify.addLinks(mTextView, Linkify.EMAIL_ADDRESSES); Linkify 使用什么颜色?在android.graphics.Co
我想将我的 android linkify 文本颜色设置为自定义颜色但是 mText.setLinkTextColor("#2f6699"); 不起作用,我一直在寻找一个内置的编译方法一个十六进制值,
我使用了这个链接中找到的方法 Android: Linkify TextView public static void addLink(TextView textView, String patter
我有这个代码: import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apac
我有一个包含 2 个 TextView 和一个 ImageView 的 Activity 。第一个 textview 包含标题,第二个包含描述和在 imageview 中对应的图像。在描述部分,如果遇
我试图在我的 Android 应用程序中链接电子邮件地址,但没有成功。 方法一: 方法二: Linkify.addLinks((TextView)view.findViewById(R.id.ema
我对 TextView 和自动链接功能有疑问。 我的应用程序中有一个关于屏幕,其中包含一些信息,如支持电话号码、电子邮件地址、网站 URL 和应用程序版本,如 01.01.01 在 textView
大家好,我有以下代码: Linkify.addLinks(tv, Pattern.compile("http://www.abc.com"),"http://www.abc.com?m=signup"
尝试做一些相当简单的事情。 像这样获取文本 User Name: This is a comment I am making 它位于单个 TextView 中。我想让用户名成为一个链接。我决定最简单的
您好,我正在使用以下代码来链接电子邮件地址和 URL,它正在按我需要的方式工作,但是如果您单击除 URL 和电子邮件之外的任何地方,未链接的白色文本会变成黑色且不可读。没什么大不了的,只是有些烦人,我
我是一名优秀的程序员,十分优秀!