Activity的跳转-Android Studio(Java)

前言

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);
            }
        });

二、最后

笔记,仅作参考

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值