You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio实现点击CardView跳转至指定网站的方法求助

实现方法

直接修改onClick方法中的switch分支,添加ACTION_VIEW类型的Intent即可,你已经提前导入了所需的android.content.Intent和android.net.Uri包,无需额外引入依赖。

修改后的完整onClick代码示例

@Override
public void onClick(View v) {
    Intent i;
    switch (v.getId()){
        case R.id.card1:
            i = new Intent(this, MainActivity.class);
            startActivity(i);
            break;
        case R.id.card2:
            // 替换为你要跳转的链接,注意必须带http/https前缀
            i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
            startActivity(i);
            break;
        case R.id.card3:
            // 替换为menu3对应的目标链接
            i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
            startActivity(i);
            break;
    }
}

注意事项

  • 跳转的链接必须携带http://或https://前缀,否则系统无法识别链接类型会导致崩溃
  • 如果安装了多个浏览器,系统会自动弹出选择弹窗让用户选择打开工具,默认会调用系统设置的默认浏览器
  • Android 11及以上系统不需要额外声明网络权限即可实现跳转外部浏览器的操作

内容的提问来源于stack exchange,提问作者Zeint

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:06:04