Android Activity页面跳转与页面传值

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android Activity页面跳转与页面传值

曲幽   2022-05-23 我要评论

概述

Android开发少不了的就是页面之间的跳转,或者想要呼叫打开其他应用等

Intent

Intent是Android程序中各组件之间进行交互的一种重要方式,不仅可以指明当前组件想要执行的运作,还可以在不同组件之间传递数据。

显示Intent启动

第一个参数为启动活动的上下文

第二个参数为想要启动的目标活动

Intent intent = new Intent(MainActivity.this, TabHostActivity.class);
startActivity(intent);

通过这个构造函数就可以构建出Intent的“意图”,且目标明确,所以为显示启动

隐式Intent启动

根据 <intent-filter> 中设定的 action 和 category 来启动,且只有<action>和<category>中的内容同时能够匹配上时,这个活动才能响应。

Intent intent = new Intent("com.zqunyan.zgstudy.ACTION_START");
intent.addCategory("com.zqunyan.zgstudy.MY_CATEGORY");
startActivity(intent);

如果 <intent-filter> 中的 category 值是 android.intent.category.DEFAULT 则可以省略addCategory(),因为DEFAULT是一种默认的 category,在调用 startActivity() 方法的时候会自动添加到 intent 中,即

Intent intent = new Intent("com.zqunyan.zgstudy.ACTION_START");
startActivity(intent);

启动其他程序

使用隐式 Intent,我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动。

网页浏览

action指定为Intent.ACTION_VIEW,其常量值为 android.intent.action.VIEW

然后将网页地址转换成 Uri 对象传递进去

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);

拨号界面

action指定为 Intent.ACTION_DIAL

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);

根据包名打开软件

借助androdi内部的 PackageManager 来根据包名取得应用的启动页面

Intent intent = getPackageManager().getLaunchIntentForPackage("com.zqunyan.zgwidget");
if(intent != null){
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

根据类名打开界面

常用于打开系统设置界面,用于一些快捷功能设置。借助 Component 来实现

如:下面实例打开华为手机的设置页面

Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.Settings$DisplaySettingsActivity");
intent.setComponent(componentName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

startActivityForResult

主页面

跳转按钮点击事件

Intent intent = new Intent(MainActivity.this, ReturnValueActivity.class);
//第二个参数用于处理返回结果是判断是哪个语句调用的
startActivityForResult(intent, 1); //requestCode = 1

处理返回结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(requestCode == 1){
        if(resultCode == 1){
            String content = intent.getStringExtra("content");
            lblStatus.setText("success");
    	}else{
            lblStatus.setText("fail");
        }
    } 
}

跳转界面

跳转界面传回返回值,并关闭界面

实例化一个空的 Intent绑定数据到 IntentsetResult() 回传结果值和绑定了数据的 Intent关闭自身,主画面接收返回结果

Intent intent = new Intent();
intent.putExtra("content", txtMessage.getText().toString());
//setResult第一个参数为结果码,常用的有Activity.RESULT_OK、RESULT_CANCELED或者自定义整数型结果码
// 第二个参数为返回值,返回值封装在Intent中
setResult(1, intent); 
finish();

页面传值

Intent.putExtra 传值

传值

intent.putExtra("uname", "admin");

取值

Intent intent = getIntent();
String name = intent.getStringExtra("uname")

借助 Bundle 传值

用于在 Activity 之间传送值或数组资料,好处是当一个页面跳转多个页面时,可以共用bundle。

简单值

打包

Bundle bundle = new Bundle();
bundle.putString("NAME", "QY");
bundle.putInt("AGE", 18);
bundle.putDouble("TALL", 175.86);
bundle.putStringArrayList("lstFilePaths", lstFilePaths);
intent.putExtras(bundle);

收包

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String strName = bundle.getString("NAME");
int intAge = bundle.getInt("AGE");
ArrayList<String> lstFilePaths = bundle.getStringArrayList("lstFilePaths");

StringArrayList

正常传值都为单个实际值,如果想要传送列表数据则一般通过构造函数或参数传递,直接通过 bundle 只能传送 StringArrayList 简单列表类型

自定义数据类

首先将自定义的数据类序列化,即继承 implements Serializable 接口

数据类

public class GoodsInfoModule implements Serializable {}

传值

bundle.putSerializable("goodsInfo", goodsInfo);

取值

goodsInfo = (GoodsInfoModule) bundle.getSerializable("goodsInfo");

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们