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

如何改写代码实现通过Chrome自定义标签页打开链接?

改用Chrome自定义标签页打开链接的实现方案

第一步:添加依赖

在你的Module级build.gradle(一般是app/build.gradle)的dependencies代码块中,添加Chrome自定义标签页的依赖:

implementation "androidx.browser:browser:1.6.0"

注:可以替换为最新的稳定版本号

第二步:修改gotoUrl方法

替换你原来的gotoUrl方法,改用CustomTabsIntent来启动链接:

首先在MainActivity顶部导入所需类:

import androidx.browser.customtabs.CustomTabsIntent;
import android.net.Uri;

然后修改gotoUrl方法:

private void gotoUrl(String s) {
    Uri uri = Uri.parse(s);
    // 构建自定义标签页Intent
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
    // 启动自定义标签页
    customTabsIntent.launchUrl(this, uri);
}

你的点击事件部分不需要修改,保持原样即可:

ImageView playtowin;

playtowin = findViewById(R.id.ptw);

playtowin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        gotoUrl("https://www.xbox.com/pt-BR/play");
    }
});

这样修改后,点击按钮就会通过Chrome自定义标签页打开指定链接,而不是直接跳转至Chrome应用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 14:46:01