简体   繁体   English

从android工作室中的按钮单击访问URL

[英]Accessing a url from a button click in android studio

So I have been trying to access a simple webpage from a button click in my app on android studio. 所以我一直试图通过android studio上我的应用程序中的按钮点击访问一个简单的网页。 Every method that I have tried has crashed the app when I have tried to run it. 当我尝试运行它时,我尝试过的每种方法都会使应用程序崩溃。 Basically I just want to click the button and then have a webpage pop up such as google. 基本上我只想点击按钮,然后弹出一个网页,如谷歌。 Right now I have a currently broken method that I made and am trying to use within the button click but it does not work. 现在我有一个当前破碎的方法,我尝试在按钮单击中使用但它不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

public void openWebURL( String inURL ) {
    Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) );

    startActivity( browse );
}

public void buyButtonOnClick(View view){
    openWebURL("www.google.com");
}

You will need to launch ACTION_VIEW intent with a correctly formatted url. 您需要使用格式正确的URL启动ACTION_VIEW意图。

public void showExternalBrowser(Activity activity, Uri uri) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(uri);
    activity.startActivity(i);
}

showExternalBrowser(activity, Uri.parse("https://www.google.com"));

Note the scheme (https) and host (www.google.com) combined make a valid url. 请注意, 方案 (https)和主机 (www.google.com)合并为一个有效的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM