首先准备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>