Activity的跳转
前言
activity的跳转实例
一、步骤
总步骤
1:新建两个activity
2:新建三个按钮做测试
<!--显性跳转!-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/intent"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/intent"
android:textSize="50sp">
</TextView>
</com.google.android.material.bottomnavigation.BottomNavigationView>
<!--隐性跳转1!-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/intent2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/intent2"
android:textSize="50sp">
</TextView>
<!--隐性跳转2!-->
</com.google.android.material.bottomnavigation.BottomNavigationView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/intent3"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/intent2"
android:textSize="50sp">
</TextView>
</com.google.android.material.bottomnavigation.BottomNavigationView>
string.xml文件
<string name="intent">显性调准1!</string>
<string name="intent2">隐性跳转2!</string>
3.绑定包
package com.example.test2;
//包
4.导入
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.bottomnavigation.BottomNavigationView;
//导入
5.创建变量
BottomNavigationView intent,intent2,intent3;
//为三个按钮创建三个变量
//2是隐性转换
1.显性跳转
//显性跳转
intent.findViewById(R.id.intent);
//变量绑定layout控件ID
//控件变量调用监听方法,创建一个新的view监听方法
intent.setOnClickListener(new View.OnClickListener() {
@Override
//设置点击监听
//onClick方法
public void onClick(View v) {
//显性跳转
//java中示例和对象需要声明出来
Intent intentAc = new Intent(MainActivity.this,intentActivity.class);
//两个参数:当前activity、需要跳转的activity
startActivity(intentAc);
//调用activity本身的共有函数方法,绑定对象
}
});
2.隐性跳转
1.manifest方式
查看manifest文件,修改其中一个,添加action和category属性
<activity
android:name=".hideIntent"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.intent" />
<category android:name="android.intent.category.intentCatgory" />
<category android:name="android.intent.category.DEFAULT" />
<!--category可以有多条,action只能有一条-->
<!--category需要设置为default-->
</intent-filter>
</activity>
<activity
android:name=".intentActivity"
android:exported="true"/>
//隐性跳转1
intent2.findViewById(R.id.intent2);
intent2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过manifest文件注册的activity属性进行跳转
//绑定action
Intent intentAc2 = new Intent("android.intent.action.intent");
//绑定category
intentAc2.addCategory("android.intent.category.DEFAULT");
//可重复增加多个category
startActivity(intentAc2);
}
});
2.Uri跳转浏览器
//隐性跳转2
intent3.findViewById(R.id.intent3);
intent3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentAc3 = new Intent(Intent.ACTION_VIEW);
//匹配ACTION_VIEW,默认为浏览器,可自定义一个webView
intentAc3.setData(Uri.parse("https://bing.com"));
//跳转到浏览器
startActivity(intentAc3);
}
});