将 TextView 中的关键字链接到文件/目录

发布于 2024-09-17 11:55:37 字数 810 浏览 2 评论 0原文

有什么方法可以将 TextView 中的关键字链接到用户 SD 卡上的文件或目录吗?我的应用程序崩溃时会生成堆栈跟踪文件,我希望用户能够单击“关于”对话框中的链接来查看最新的文件或包含所有这些文件的文件夹。 (类似于“如果此应用程序崩溃,请将[链接]最新的 stack.trace 文件[/链接]发送给我们 [电子邮件受保护]。")

我知道可以使用以下代码来创建 Web 链接,但我尝试将其修改为使用“file:///sdcard/path” /to/file/stack.trace”,这会导致我的应用程序在单击链接时强制关闭。

<string name="strSample">This is Web link to &lt;a href=&quot;http://www.google.com&quot;>Google&lt;/a>!</string>

final TextView tvSample = (TextView)findViewById(R.id.tvSample);
tvSample.setMovementMethod(LinkMovementMethod.getInstance());
String strSample = getResources().getString(R.string.strSample);
tvSample.setText(Html.fromHtml(strSample));

Is there any way that I can link a keyword in a TextView to a file or directory on the user's SD card? My app produces stack trace files when it crashes and I want the user to able to click a link in my About dialog to view either the latest one or the folder containing all of them. (Something like "If this app crashes, please send [link]the latest stack.trace file[/link] to us at [email protected].")

I know it is possible to use the following code to make a Web link but I tried to modify it to use "file:///sdcard/path/to/file/stack.trace" which causes my app to Force Close when the link is clicked.

<string name="strSample">This is Web link to <a href="http://www.google.com">Google</a>!</string>

final TextView tvSample = (TextView)findViewById(R.id.tvSample);
tvSample.setMovementMethod(LinkMovementMethod.getInstance());
String strSample = getResources().getString(R.string.strSample);
tvSample.setText(Html.fromHtml(strSample));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-09-24 11:55:39

您可以通过替换返回 SpannableHtml.fromHtml(strSample) 部分来扩展您的方法。创建您自己的 Spannable 并向其中添加 ClickableSpan。在 onClick 方法中,您可以放置​​所需的任何逻辑。这是您可以扩展的快速示例

public Spannable getProfileLink(final Context context) {
    final String name = firstName + " " + lastName;
    Spannable spans = SpannableStringBuilder.valueOf(name);
    ClickableSpan clickSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            // TODO - call profile here
            Toast.makeText(context, "Will call profile", Toast.LENGTH_LONG);
        }

    };
    spans.setSpan(clickSpan, 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spans;
}

You can extend your method by replacing Html.fromHtml(strSample) part which returns Spannable. Create your own Spannable and add ClickableSpan to it. In the onClick method you can place whatever logic you need. Here's quick examle that you can extend

public Spannable getProfileLink(final Context context) {
    final String name = firstName + " " + lastName;
    Spannable spans = SpannableStringBuilder.valueOf(name);
    ClickableSpan clickSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            // TODO - call profile here
            Toast.makeText(context, "Will call profile", Toast.LENGTH_LONG);
        }

    };
    spans.setSpan(clickSpan, 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spans;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文