kotlin - 使用 Compose for Desktop 在浏览器中打开链接
问题描述
如果单击按钮,如何在浏览器中打开链接。我正在为此使用Compose for Desktop。
Button(onClick = {
// What I have to write here..
}) {
Text(
text = "Open a link",
color = Color.Black
)
}
提前致谢。
解决方案
使用Desktop#browse(URI)方法。它在用户的默认浏览器中打开一个 URI。
public static boolean openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public static boolean openWebpage(URL url) {
try {
return openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
return false;
}
推荐阅读
- python - 我想要一个 pandas 脚本根据第一个电子表格中的值将一个 excel 表中的值排列到另一个表中
- apache - Apache 中的 TLS 相互身份验证 - 如何获取经过身份验证的用户的用户名
- angular - Angular - 基于服务更新视图的最佳方式(性能)
- c++ - fanotify:如何检查内核是否启用或禁用了 fanotify API
- python - 在 ACI 上部署机器学习模型时出现 ModelNotFoundError
- arrays - Snowflake - 将 JSON 数组字符串对象值提取为管道分隔值
- python - 是否可以在pycharm的python中包含c代码?
- powershell - 如何检查是否安装了chrome扩展
- apache-spark - 不安全模式下的 Elasticsearch pyspark 连接
- javascript - 计算特定 y 坐标的圆边缘的两个 x 坐标