国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】

這篇具有很好參考價(jià)值的文章主要介紹了使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

?

目錄

Intent 類型

一、顯式Intent方法

二、隱式Intent方法


開發(fā)文檔鏈接:Intent 和 Intent 過濾器 ?|? Android 開發(fā)者 ?|? Android Developers (google.cn)https://developer.android.google.cn/guide/components/intents-filters

Intent是一個(gè)消息傳遞對象,解決Android應(yīng)用的各項(xiàng)組件之間的通訊,您可以用來從其他應(yīng)用組件請求操作。盡管 Intent 可以通過多種方式促進(jìn)組件之間的通信,但其基本用例主要包括以下三個(gè):

  • 啟動(dòng) Activity,即實(shí)現(xiàn)在幾個(gè)Activity之間的跳轉(zhuǎn)
  • 啟動(dòng)服務(wù)
  • 傳遞廣播

Intent 類型

Intent 分為兩種類型:

  • 顯式 Intent:通過提供目標(biāo)應(yīng)用的軟件包名稱或完全限定的組件類名來指定可處理 Intent 的應(yīng)用。通常,您會(huì)在自己的應(yīng)用中使用顯式 Intent 來啟動(dòng)組件,這是因?yàn)槟酪獑?dòng)的 Activity 或服務(wù)的類名。例如,您可能會(huì)啟動(dòng)您應(yīng)用內(nèi)的新 Activity 以響應(yīng)用戶操作,或者啟動(dòng)服務(wù)以在后臺(tái)下載文件。
  • 隱式 Intent?:不會(huì)指定特定的組件,而是聲明要執(zhí)行的常規(guī)操作,從而允許其他應(yīng)用中的組件來處理。例如,如需在地圖上向用戶顯示位置,則可以使用隱式 Intent,請求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。

本文主要介紹第一個(gè)用法,通過顯式和隱式分別啟動(dòng) Activity,即實(shí)現(xiàn)在幾個(gè)Activity之間的跳轉(zhuǎn)。

作好準(zhǔn)備工作,新建一個(gè)項(xiàng)目,命名為MyAppTest_1。

先來構(gòu)建兩個(gè)Activity。分別命名為MainActivity(主界面)和SecondActivity(跳轉(zhuǎn)之后的界面)。

activity_main.xml中添加兩個(gè)按鈕。

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_1"
        android:text="顯式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btn_2"
        android:text="隱式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/>

</LinearLayout>

activity_second.xml中添加一個(gè)字段以示區(qū)分。

<?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"
    tools:context=".SecondActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

</LinearLayout>

一、顯式Intent方法

1. 1? MainActivity.java??完整代碼

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //跳轉(zhuǎn)頁面的按鈕,顯式Intent。
        //通過findViewById獲取按鈕,并定義變量名為bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //顯式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

    }
}

這是其中的第一種寫法。


        //跳轉(zhuǎn)頁面的按鈕,顯式Intent。
        //通過findViewById獲取按鈕,并定義變量名為bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //顯式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

還有第二種寫法,將上方代碼塊替換為如下。


        //跳轉(zhuǎn)頁面的按鈕,顯式Intent。
        //通過findViewById獲取按鈕,并定義變量名為bt_change_2
        Button bt_change_2 =(Button)this.findViewById(R.id.btn_1);
        bt_change_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

最后運(yùn)行界面及結(jié)果:

使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】

二、隱式Intent方法

隱式 Intent 指定能夠在可以執(zhí)行相應(yīng)操作的設(shè)備上調(diào)用任何應(yīng)用的操作。如果您的應(yīng)用無法執(zhí)行該操作而其他應(yīng)用可以,且您希望用戶選取要使用的應(yīng)用,則使用隱式 Intent 非常有用。

Q:? 那么是怎樣正確找到并打開目標(biāo)活動(dòng)呢?

Intent的發(fā)送者在構(gòu)造Intent對象時(shí),并不需要指定“接收者”,而是通過一定的設(shè)置,由系統(tǒng)進(jìn)行篩選,這有助于解耦。隱式Intent需要借助Intent Filter來實(shí)現(xiàn)“篩選”這一過程,并且僅當(dāng)隱式 Intent 可以通過 Intent 過濾器之一傳遞時(shí),系統(tǒng)才會(huì)將該 Intent 傳遞給應(yīng)用組件。
?

意思就是說,當(dāng)且僅當(dāng)該 “Intent對象” 與 “在清單文件中為目標(biāo)活動(dòng)創(chuàng)建的action和category” 相一致時(shí),即調(diào)用該目標(biāo)活動(dòng)。

<action>為目標(biāo)活動(dòng)地址,而<category>標(biāo)簽則包含了一些附加信息,更精確地指明了當(dāng)前的活動(dòng)能夠響應(yīng)的Intent中還可能帶有的category。只有<action>和<category>中的內(nèi)容同時(shí)能夠匹配上
Intent中指定的action和 category時(shí),這個(gè)活動(dòng)才能響應(yīng)該Intent。
?


下面是具體的做法。

由此引出,我們要注意,在使用隱式調(diào)用時(shí),需要先在AndroidManifest.xml(清單文件)中對需要打開的Activity進(jìn)行定義。

2.1?AndroidManifest.xml 完整代碼

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapptest_1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyAppTest_1">

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我們做了什么呢,其他的都沒有變,僅添加了以下代碼:

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

幾個(gè)問題:

  1. 為什么在<action android:name="com.example.myapptest_1.ACTION_START"></action>這里,最后這里是有一個(gè)</action>的方式結(jié)尾,我也嘗試了以下,改為以 />結(jié)尾程序也是可以跑通的。不知道是不是有什么特別的區(qū)別。
  2. 這里的com.example.myapptest_1.是活動(dòng)所在的地址。需要換成你對應(yīng)的路徑。

    使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】

  3. 為什么定義了兩個(gè) category,其中android.intent.category.DEFAULT是一種默認(rèn)的category, 在調(diào)用startActivity()方法的時(shí)候會(huì)自動(dòng)將這個(gè)category添加到 Intent中。第二個(gè)是自己定義的。
  4. 注意:如果當(dāng)前組件是Activity,并且沒有指定的category,必須加上category并使用默認(rèn)的DEFAULT?。?!
    <category android:name="android.intent.category.DEFAULT" />
    
  5. 在<action>和<category>后面這些全大寫的指令是什么意思,這些,可以搜一下對應(yīng)的源碼。只需要跳轉(zhuǎn)頁面的話,就按照這個(gè)樣子寫是可以的。
  6. 注意:
  • 一個(gè)Activity可以指定多個(gè)action屬性
  • 一個(gè)Activity也可以指定多個(gè)category屬性
  • 一個(gè)Intent只能指定一個(gè)action
  • 一個(gè)intent可以指定多個(gè)category

2. 2? MainActivity.java??完整代碼

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //跳轉(zhuǎn)頁面的按鈕,隱性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隱性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            intent.addCategory("com.example.myapptest_1.MY_CATEGORY");
            this.startActivity(intent);
        });

    }
}

幾個(gè)問題:

若沒有指定的category,則不需要添加

intent.addCategory("com.example.myapptest_1.MY_CATEGORY");

只需要如下指定action即可,就是說這樣寫也是可行的,不會(huì)報(bào)錯(cuò),但不建議。

        //跳轉(zhuǎn)頁面的按鈕,隱性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隱性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            this.startActivity(intent);
        });

那么我們跳轉(zhuǎn)到了新的界面怎么返回呢?

下面是返回代碼示例,我們在 SecondActivity.java 中編寫。

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button gobackBtn = findViewById(R.id.button_back);
        gobackBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回上一個(gè)頁面
                finish();
            }
        });

    }
}

相對應(yīng)的activity_second.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"
    tools:context=".SecondActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="back"/>

</LinearLayout>

界面如圖。

使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】文章來源地址http://www.zghlxwxcb.cn/news/detail-417564.html

到了這里,關(guān)于使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 小程序與h5之間的跳轉(zhuǎn)(webview)

    在微信官網(wǎng)中,h5跳小程序并帶參是有api的,直接使用wx.miniProgram.navigateTo(url)就可以了,但是項(xiàng)目中往往這個(gè)是不夠用的,因?yàn)槲覀兛赡苓€想著從小程序中執(zhí)行完一些程序后拿到返回值給返回到h5中,這時(shí)這個(gè)參數(shù)就不好帶到h5中了,所以這篇文章就解決了這個(gè) 小程序向h5傳參

    2024年02月10日
    瀏覽(19)
  • Android頁面跳轉(zhuǎn)(Intent)

    Android頁面跳轉(zhuǎn)(Intent)

    布局 代碼 隱式意圖 沒有明確指定組件名的Intent為隱式意圖,系統(tǒng)會(huì)根據(jù)隱式意圖中設(shè)置的動(dòng)作(action)、類別(category)、數(shù)據(jù)UIL等來匹配最合適的組件。 首先在清單文件中使用意圖過濾器設(shè)置活動(dòng)的名字 action android:name=“HomeActivity” / category android:name=“android.intent.categ

    2023年04月09日
    瀏覽(24)
  • js有哪些常用的跳轉(zhuǎn)頁面方法(補(bǔ))

    在JavaScript中,常用的跳轉(zhuǎn)頁面方法包括: 使用location對象的href屬性跳轉(zhuǎn)頁面: 使用location對象的replace方法跳轉(zhuǎn)頁面(不會(huì)在瀏覽器歷史記錄中留下記錄): 使用window對象的open方法打開新窗口或標(biāo)簽頁: 使用a標(biāo)簽的click方法模擬點(diǎn)擊跳轉(zhuǎn): 使用setTimeout函數(shù)延時(shí)跳轉(zhuǎn)頁面:

    2024年04月09日
    瀏覽(24)
  • ARMv8/ARMv9架構(gòu)下特權(quán)程序之間的跳轉(zhuǎn)模型與系統(tǒng)啟動(dòng)探析

    ARMv8/ARMv9架構(gòu)下特權(quán)程序之間的跳轉(zhuǎn)模型與系統(tǒng)啟動(dòng)探析

    ARMv8和ARMv9架構(gòu)是ARM公司推出的先進(jìn)處理器架構(gòu),被廣泛應(yīng)用于移動(dòng)設(shè)備、服務(wù)器和嵌入式系統(tǒng)。這兩個(gè)架構(gòu)的設(shè)計(jì)旨在提供更高的性能、更好的能效以及更強(qiáng)大的安全性。其中,不同特權(quán)程序之間的跳轉(zhuǎn)模型是這一架構(gòu)中關(guān)鍵的組成部分,對于系統(tǒng)的整體安全性和可靠性具有

    2024年03月16日
    瀏覽(17)
  • Android Studio:Intent與組件通信實(shí)現(xiàn)頁面跳轉(zhuǎn)功能

    Android Studio:Intent與組件通信實(shí)現(xiàn)頁面跳轉(zhuǎn)功能

    ??Android Studio 專欄正在持續(xù)更新中,案例的原理圖解析、各種模塊分析??這里都有哦,同時(shí)也歡迎大家訂閱專欄,獲取更多詳細(xì)信息哦??? ?個(gè)人主頁:零小唬的博客主頁 ??歡迎大家 ??點(diǎn)贊 ??評論 ??收藏 ?作者簡介:20級(jí)計(jì)算機(jī)專業(yè)學(xué)生一枚,來自寧夏,可能會(huì)去

    2024年02月05日
    瀏覽(25)
  • 微信小程序頁面的跳轉(zhuǎn)和導(dǎo)航的配置和vant組件

    微信小程序頁面的跳轉(zhuǎn)和導(dǎo)航的配置和vant組件

    結(jié)論: navigateTo ,? redirectTo ?只能打開非 tabBar 頁面。 switchTab ?只能打開 tabBar 頁面。 reLaunch ?可以打開任意頁面。 頁面底部的 tabBar 由頁面決定,即只要是定義為 tabBar 的頁面,底部都有 tabBar。 調(diào)用頁面路由帶的參數(shù)可以在目標(biāo)頁面的 onLoad 中獲取。 (1)當(dāng)我們使用 redirectTo跳

    2024年02月09日
    瀏覽(28)
  • 微信小程序開發(fā)系列(十一)·小程序頁面的跳轉(zhuǎn)設(shè)置以及參數(shù)傳遞

    微信小程序開發(fā)系列(十一)·小程序頁面的跳轉(zhuǎn)設(shè)置以及參數(shù)傳遞

    目錄 1.? 跳轉(zhuǎn)到商品列表 1.1??url: 當(dāng)前小程序內(nèi)的跳轉(zhuǎn)鏈接 1.2??navigate:保留當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面。但是不能跳到 tabbar 頁面 1.3??redirect: 關(guān)閉當(dāng)前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個(gè)頁面。但不能跳轉(zhuǎn)到 tabbar 頁面 1.4??navigate和redirect的區(qū)別 1.5? switchTab:跳轉(zhuǎn)到 t

    2024年04月12日
    瀏覽(26)
  • Spring-mvc的參數(shù)傳遞與常用注解的解答及頁面的跳轉(zhuǎn)方式---綜合案例

    Spring-mvc的參數(shù)傳遞與常用注解的解答及頁面的跳轉(zhuǎn)方式---綜合案例

    目錄 一.slf4j--日志 二.常用注解 ??? ? ?2.1.@RequestMapping ??????2.2.@RequestParam ??????2.3.@RequestBody ??????2.4.@PathVariable 三.參數(shù)的傳遞 3.1 基礎(chǔ)類型 3.2 復(fù)雜類型 3.3?@RequestParam 3.4? @PathVariable 3.5 @RequestBody 3.6 增刪改查? 四.返回值? ????????? 4.1 void 返回值? ?4.2 String

    2024年02月09日
    瀏覽(28)
  • iOS-自定義Intent及ShortCut,能通過快捷指令喚醒APP并跳轉(zhuǎn)到指定頁面

    iOS-自定義Intent及ShortCut,能通過快捷指令喚醒APP并跳轉(zhuǎn)到指定頁面

    創(chuàng)建完成后,在intents文件中勾選以下target,否則在擴(kuò)展里,無法訪問到前面創(chuàng)建的Intent類 編譯程序,Xcode 會(huì)自動(dòng)生成對應(yīng)的類,我這里的話會(huì)生成 LaunchAppIntent 類,下面需要使用 引入上述生成的文件,并寫入代理,如果之前未勾選target是無法引入的 在代理中寫入,以及點(diǎn)擊

    2024年01月23日
    瀏覽(47)
  • 【微信小程序】通過使用 wx.navigateTo方法進(jìn)行頁面跳轉(zhuǎn),跳轉(zhuǎn)后的頁面中通過一些方式回傳值給原頁面

    以下是幾種常見的回傳值的方式: 使用 wx.navigateTo 方法傳遞參數(shù): 在跳轉(zhuǎn)時(shí),可以在目標(biāo)頁面的 URL 中攜帶參數(shù),然后在目標(biāo)頁面的 onLoad 方法中獲取參數(shù),并在目標(biāo)頁面中進(jìn)行處理。例如: 在目標(biāo)頁面的 onLoad 方法中獲取參數(shù): 使用 wx.navigateBack 方法回傳值: 在目標(biāo)頁面中

    2024年02月13日
    瀏覽(90)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包