android 按钮打开浏览器浏览网页链接

android 版本:2021.2.1

例程:openweblin

关键代码是按钮的响应代码:

public void openWeblink(View view) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("url"));//Url 要打开的网址
        intent.setAction(Intent.ACTION_VIEW);
        this.startActivity(intent); //启动浏览器
    }

例程就是在空白项目里加了一个按钮,其他不动,代码如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开网页"
        android:onClick="openWeblink"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.37" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.openweblink;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void openWeblink(View view) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("https://blog.csdn.net/kim5659/article/details/121943119?spm=1001.2014.3001.5501"));
        intent.setAction(Intent.ACTION_VIEW);
        this.startActivity(intent); //启动浏览器
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要在Android Studio中打开网页,可以使用WebView控件。WebView是一个可以在应用程序中显示网页的控件,可以加载HTML、CSS、JavaScript和图像等网页内容。 以下是在Android Studio中打开网页的步骤: 1. 在布局文件中添加WebView控件。 2. 在Java代码中获取WebView控件的引用。 3. 使用WebView控件的loadUrl()方法加载网页。 4. 在AndroidManifest.xml文件中添加Internet权限。 例如,以下代码可以在WebView控件中加载Google网页: 布局文件: ``` <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` Java代码: ``` WebView webView = (WebView) findViewById(R.id.webview); webView.loadUrl("https://www.google.com"); ``` AndroidManifest.xml文件: ``` <uses-permission android:name="android.permission.INTERNET" /> ``` ### 回答2: 在Android Studio中打开网页是一个相对简单的过程。有几种方法可以在应用程序中打开网页,包括在应用程序中使用WebView或将网页发送到用户的默认浏览器。下面是一些详细的指导步骤。 1. 使用WebView在应用程序中打开网页。 在您的项目中添加一个WebView组件,您可以通过从菜单中选择“File”->“New”->“XML”->“Layout XML File”来完成这个步骤。您需要为WebView设置布局参数,以便您可以在应用程序的界面上正确地展示它。您可以在onCreate()方法中获取一个WebView组件的引用并打开您需要的网页。以下是代码示例。 ``` WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.example.com"); ``` 2. 将网页发送到用户的默认浏览器。 如果您想用默认的浏览器打开而不是在应用程序中使用WebView,您可以使用以下代码实现。 ``` String url = "http://www.example.com"; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); ``` 以上是两种在Android Studio中打开网页的方法。选择使用哪种方案需要根据您的实际需求和应用程序的特点来做决定。 ### 回答3: Android Studio是一个流行的集成开发环境,用于为Android设备编写应用程序。在Android Studio中打开网页可以通过许多方法实现,具体取决于您的需求。 一种方法是使用WebView Widget。WebView是一个内置的类,可在应用程序中嵌入Web内容。使用该类可以在Android Studio中打开网页。要使用此方法,必须在XML布局中添加WebView,并且可以使用以下代码在活动中加载网页: ```java WebView myWebView = findViewById(R.id.webView); myWebView.loadUrl("https://www.example.com"); ``` 另一种使用Intents。Intent是Android系统中的重要组件之一,用于启动Activity或服务。通过创建一个意图并指定URL,可以使用Intent在浏览器打开网页。以下是启动默认浏览器的例子: ```java String url = "https://www.example.com"; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); ``` 如果您想使用特定的浏览器,可以使用以下代码: ```java String url = "https://www.example.com"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.setPackage("com.android.chrome"); startActivity(intent); ``` 在这个例子中,我们指定了Chrome浏览器的包名,以确保特定的浏览器得到被启动的优先权。 还有许多其他方法可以在Android Studio中打开网页。例如,您可以使用WebViewClient处理WebView上发生的事件,或使用WebChromeClient处理浏览器控件上发生的事件。无论您选择哪种方法,它们都将让您的应用程序在Android设备上具有更强大的Web浏览能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kim5659

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值