日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?Activity活動頁面跳轉與頁面傳值_Android

作者:曲幽 ? 更新時間: 2021-12-10 編程語言

概述

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

原文鏈接:https://blog.csdn.net/ymtianyu/article/details/121789059

欄目分類
最近更新