将 TextView 中的关键字链接到文件/目录
有什么方法可以将 TextView 中的关键字链接到用户 SD 卡上的文件或目录吗?我的应用程序崩溃时会生成堆栈跟踪文件,我希望用户能够单击“关于”对话框中的链接来查看最新的文件或包含所有这些文件的文件夹。 (类似于“如果此应用程序崩溃,请将[链接]最新的 stack.trace 文件[/链接]发送给我们 [电子邮件受保护]。")
我知道可以使用以下代码来创建 Web 链接,但我尝试将其修改为使用“file:///sdcard/path” /to/file/stack.trace”,这会导致我的应用程序在单击链接时强制关闭。
<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));
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过替换返回
Spannable
的Html.fromHtml(strSample)
部分来扩展您的方法。创建您自己的Spannable
并向其中添加ClickableSpan
。在onClick
方法中,您可以放置所需的任何逻辑。这是您可以扩展的快速示例You can extend your method by replacing
Html.fromHtml(strSample)
part which returnsSpannable
. Create your ownSpannable
and addClickableSpan
to it. In theonClick
method you can place whatever logic you need. Here's quick examle that you can extend