TextView中的Android HTML可下载文件链接不起作用



我在Android HTML中使用的<a>标签上看到了很多答案。

我对<a href='example.com/test.json' download />有一个问题,我在简单的HTML代码中尝试过,它会自动从浏览器下载。

但当我在Android中尝试同样的东西时,它不起作用,只是在浏览器中显示数据。

我想使用与web版本相同的<a>标签下载该文件。有可能做到吗?

我在互联网上找到的示例JSON,并在这里用<a>标记编写。

<a href="https://einvoice1-trial.nic.in/Documents/SampleJson.json" target="_blank" download>Sample File</a>

使用java代码:

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='example.com/test.json' download> Download Now </a>";
textView.setText(Html.fromHtml(text)); 
// From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int)
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

或者在布局XML文件中,在TextView小部件属性中

android:autoLink="web"
android:linksClickable="true

最新更新