Android实现简单的网页跳转

简介: 首先准备Android studio 工具第二直接上代码

首先准备Android studio 工具

第二直接上代码

MainActivity:

package com.jay.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private TextView web_one;
    private WebView web_two;
    //可以提前定义网络地址
    private String uri="https://www.csdn.com/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一种跳转页面的方法
        web_one=findViewById(R.id.web_one);
        web_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
        //第二种跳转页面的方法
        web_two=findViewById(R.id.web_two);
        web_two.loadUrl("https://www.csdn.com/");
        //可以写成:web_two.loadUrl(uri);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:gravity="center"
        android:text="第一种直接跳转到Activity页面,Activity
页面直接链接网页"
        />
    <TextView
        android:id="@+id/web_one"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="第一种方法" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:gravity="center"
        android:text="第二种点击按钮跳转外部网页" />
    <WebView
        android:id="@+id/web_two"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        />
</LinearLayout>

Main2Activity:

package com.jay.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //方式一:代码实现跳转
        Intent intent = new Intent();
        //通过intent发送数据
        intent.setAction("android.intent.action.VIEW");
        //创建一个链接  链接.语法解析
        Uri content_url = Uri.parse("https://www.csdn.com/");
        //通过intent接受
        intent.setData(content_url);
        //开始于当前intent
        startActivity(intent);
    }
}

activity_main2.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=".Main2Activity">
</androidx.constraintlayout.widget.ConstraintLayout>
相关文章
|
3月前
|
Android开发
【安卓版】网页转应用v1.2,生成属于你的专属应用
【安卓版】网页转应用v1.2,生成属于你的专属应用
39 2
|
3月前
|
Linux 调度 Android开发
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
46 0
|
9月前
|
设计模式 Android开发
Android scheme 跳转的设计与实现
为了解决这些问题,App 一般都会自定义一个 scheme 跳转协议,多端都实现这个协议,以此来解决各种运营需求。今天就来解析下 QMUI 最新版 QMUISchemeHandler 的设计与实现。
133 0
|
Android开发
flutter中实现仿Android端的onResume和onPause方法
flutter中实现仿Android端的onResume和onPause方法
|
4月前
|
小程序 JavaScript 前端开发
微信小程序(十七)小程序监听返回键跳转事件(安卓返回也适用)
onUnload:function(){ wx.redirectTo({ url: '../index/index' }) wx.navigateTo({ url: '../index/index' }) wx.switchTab({ url: '../../member/member' }) }
297 0
|
3月前
|
Linux 调度 Android开发
Kernel怎么跳转到Android:linux与安卓的交界
Kernel怎么跳转到Android:linux与安卓的交界
35 0
|
4月前
|
Android开发
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
41 0
|
4月前
|
Java C# Android开发
Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动
为了帮助用户在使用 APP 时提高其稳定性和使用体验,有时候我们需要让安卓手机的界面跳转到手机自带的自启动管理界面,以此来引导用户将 APP 加入自启动,确保应用程序在后台运行时不被系统杀死,从而保证应用程序服务的稳定性和可靠性。同时,这也可以提高用户的使用体验,使用户能够更好地享受应用程序的功能和服务。
96 0
Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动
|
4月前
|
XML JavaScript 前端开发
把网页放到安卓app中
把网页放到安卓app中
40 0
|
4月前
|
Android开发 Kotlin
使用kotlin 进行 安卓app 的 活动跳转 与 片段跳转
使用kotlin 进行 安卓app 的 活动跳转 与 片段跳转
46 0