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

04 |「Activity 和 Intent」

這篇具有很好參考價值的文章主要介紹了04 |「Activity 和 Intent」。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

學習 Activity、Intent、Context

一、Activity

1、繼承關(guān)系

java.lang.Object
   ?	android.content.Context
 	   ?	android.content.ContextWrapper
 	 	   ?	android.view.ContextThemeWrapper
 	 	 	   ?	android.app.Activity
  • Activity繼承自ContextThemeWrapper,可以設(shè)置主題。
  • ContextThemeWrapper繼承自ContextWrapper,封裝了Context的常用功能。
  • ContextWrapper繼承自Context,即Activity本質(zhì)上是一個Context。
  • Context定義了與Android環(huán)境交互的接口。
  • 最終都繼承自java.lang.Object。
  • 通過這種繼承,Activity就集成了Context的能力,可以訪問資源、數(shù)據(jù)庫、SharedPreferences等。
    同時作為Context的子類,Activity也可以作為Context參數(shù)被傳遞使用。
    這種繼承關(guān)系讓Activity既是一個 UI 組件,也是一個Context,很好地結(jié)合了兩者的功能

2、簡介

  • Activity 代表應用程序的單個屏幕,用戶可以使用該屏幕執(zhí)行單一、集中的任務,Activity 通常以全屏窗口的形式呈現(xiàn)給用戶;
  • Activity 是一個應用程序組件,單個用戶任務的單個屏幕;
  • 每個 Activity 都有自己的布局文件;
  • 可以為 Activity 分配父子關(guān)系,以在應用程序中啟用向上導航;
  • 一個應用程序通常由多個彼此松散的屏幕組成。每個屏幕都是一個 Activity;
  • 應用程序中有一個主Activity(MainActivity.java),在應用程序啟動時呈現(xiàn)給用戶;通過主Activity 可以啟動其他 Activity 來執(zhí)行不同的操作;
  • 每次啟動新活動,前一個 Activity 都會停止,但系統(tǒng)會將該 Activity 保留在堆棧中;當新 Activity 啟動時,該新 Activity 被推入后臺堆棧并獲取用戶焦點;當用戶完成當前 Activity 并按下后退按鈕時,該 Activity 將從堆棧中彈出并銷毀,并恢復上一個 Activity;
  • Activity 是有意圖地開始或激活的。Intent 是一條異步消息,可以在 Activity 中使用它來請求來自另一個 Activity 或某個其他應用程序組件的操作;可以使用 Intent 從一個 Activity 啟動另一個 Activity,并在 Activity 之前傳遞數(shù)據(jù);

二、Intent

  • 允許從應用程序中的另一個組件請求操作。例如,從另一個組件啟動一個 Activity;
  • 顯示 Intent:可以指示接收數(shù)據(jù)的特定目標組件;
  • Intent 附加信息是 Bundle,鍵值對;

三、Context

  • 全局信息:應用程序的資源,圖片資源、字符串資源 ;
  • 作用:用來訪問全局信息,一些常用的組件(Activity、Service)繼承自 Context(繼承后具有訪問全局資源的能力),目的是通過 Context 方便地訪問資源;
  • setText 的實現(xiàn):getContext():獲取相關(guān)的 Context 對象,getResource():獲取資源管理器,getText(id):返回資源 id 的字符串;
    // 訪問字符串資源
    public class MainActivity extends AppCompatActivity {
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            tv = new TextView(this);  // this:MainActivity的實例,將this傳給context,在內(nèi)部對context有一個引用
            tv.setText(R.string.hello_world); 
            setContentView(tv);  // 指定一個視圖
            }
    }
    
    // 訪問圖片資源
    public class MainActivity extends AppCompatActivity {
      
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           	ImageView iv = new ImageView(this);
           	iv.setImageResource(R.mipmap.ic_launcher);
            }
    }
    

四、實踐 Lab

1、需求

  • 創(chuàng)建并構(gòu)建兩個 Activity(Activity1Activity2);
  • Activity1 作為主 Activity,主要包含一個 “發(fā)送” 按鈕,當用戶點擊此按鈕將使用 Intent 來啟動 Activity2;
04 |「Activity 和 Intent」,安卓開發(fā)入門指南(注重實操),android
  • 主 Activity 中添加 EditText,用戶輸入消息,并點擊發(fā)送按鈕,主 Activity 使用 Intent 來啟動第二個 Activity 并將用戶的消息發(fā)送到第二個 Activity,第二個 Activity 顯示它接收到的消息
04 |「Activity 和 Intent」,安卓開發(fā)入門指南(注重實操),android
  • 第二個 Activity 添加 EditText 和回復按鈕;用戶鍵入回復消息并點擊回復按鈕,使用 Intent 將回復消息從第二個 Activity 傳遞給主 Activity,并顯示;
04 |「Activity 和 Intent」,安卓開發(fā)入門指南(注重實操),android

2、代碼實現(xiàn)

  • 創(chuàng)建第一個Activity 布局
// activity_main.xml(主Activity)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="16dp"
        android:textColor="@android:color/background_dark"
        android:layout_marginBottom="16dp"
        android:onClick="launchSecondActivity"
        android:text="@string/button_main" />

    <EditText
        android:id="@+id/editText_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginBottom="19dp"
        android:ems="10"
        android:inputType="text"
        android:text="Name" />
</RelativeLayout>
  • 創(chuàng)建第二個Activity 布局
// 第二個Activity
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:id="@+id/text_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="@string/text_header"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginTop="40dp"
        android:text=""
        android:textAppearance="AppCompat.Medium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_header" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 主 Activity 添加 Intent
   public void launchSecondActivity(View view) {
        // 將顯示Intent添加到主Activity,Intent用于單擊發(fā)送按鈕時激活第二個Activity
        // 參數(shù):第一個參數(shù),應用程序Context;第二個參數(shù):將接收該Intent的特定組件
        // 當點擊發(fā)送按鈕時,MainActivity發(fā)送Intent并啟動第二個Activity 出現(xiàn)在屏幕
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}
  • 從主 Activity 發(fā)送數(shù)據(jù)到第二個 Activity
    • 使用 Intent 將數(shù)據(jù)從一個 Activity 發(fā)送到另一個 Activity
    • Intent 傳遞數(shù)據(jù)到目標 Activity 的方式
      • 1)數(shù)據(jù)字段:Intent 數(shù)據(jù)指要操作的特定數(shù)據(jù)的 URl
      • 2)Intent 附加信息,如果傳遞的數(shù)據(jù)不是 URl 或想要發(fā)送多條信息,可以將附加信息放入extras 中。
      • Intent 附加信息內(nèi)容是 Bundle 。Bundle 是數(shù)據(jù)集合,存儲形式為鍵值對;
      • 從一個 Activity 傳遞信息到另一個 Activity,可以將鍵和值放入發(fā)送 Activity 的 Intent extra 中,然后在接收 Activity 中將它們?nèi)〕觯?/li>

Bundle 中包含其他數(shù)據(jù),本需求中為用戶輸入的字符串文章來源地址http://www.zghlxwxcb.cn/news/detail-623455.html

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    public static final String EXTRA_MESSAGE = "com.example.myapplication.extra.MESSAGE";
    private EditText mMessageEditText;
    public static final int TEXT_REQUEST = 1;  // 第二個Activity回復響應的鍵
    private TextView mReplyHeadTextView;  // 回復標頭Textview
    private TextView mReplyTextView;  // 回復TextView元素


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  // 指定一個視圖
        mMessageEditText = findViewById(R.id.editText_main);  // 使用findViewById()獲取對 EditText 的引用
        mReplyHeadTextView = findViewById(R.id.text_header_reply);
        mReplyTextView = findViewById(R.id.text_message_reply);
        }


    public void launchSecondActivity(View view) {
        // 將顯示Intent添加到主Activity,Intent用于單擊發(fā)送按鈕時激活第二個Activity
        // 參數(shù)1:應用程序Context和將接收該Intent的特定組件
        // 當點擊發(fā)送按鈕時,MainActivity發(fā)送Intent并啟動第二個Activity 出現(xiàn)在屏幕
        Intent intent = new Intent(this, SecondActivity.class);
        String message = mMessageEditText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivityForResult(intent, TEXT_REQUEST);

    }

    @Override
    // 回調(diào)方法
    // requestCode:請求
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 處理返回數(shù)據(jù)
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TEXT_REQUEST) {
            if (requestCode == RESULT_OK) {
                String reply =
                        data.getStringExtra(SecondActivity.EXTRA_REPLY);
                mReplyHeadTextView.setVisibility(View.VISIBLE);
                mReplyTextView.setText(reply);
                mReplyTextView.setVisibility(View.VISIBLE);
            }
        }
    }
}
  • 將數(shù)據(jù)從第二個 Activity 返回給主 Activity
    startActivity():使用顯式 Intent 啟動另一個 Activity 時,不會期望返回任何數(shù)據(jù),只是激活該 Activity;
    如果想從激活的 Activity 中獲取數(shù)據(jù),則需要以 startActivityForResult() 啟動它
public class SecondActivity extends AppCompatActivity {
    public static final String EXTRA_REPLY = "com.example.myapplication.extra.REPLY";
    private EditText mReply;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        mReply = findViewById(R.id.editText_second);
        // 獲取激活此Activit的Intent
        Intent intent = getIntent();
        // 獲取Intent extra中包含的字符串
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        // 獲取要顯示的控件的引用
        TextView textView = findViewById(R.id.text_message);
        // 通過引用在此控件上顯示獲取Intent extra中包含的字符串
        textView.setText(message);
    }

    public void returnReply(View view) {
        String reply = mReply.getText().toString();
        Intent replyIntent = new Intent();
        replyIntent.putExtra(EXTRA_REPLY, reply);
        setResult(RESULT_OK, replyIntent);
        finish();
    }
}

到了這里,關(guān)于04 |「Activity 和 Intent」的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Android kotlin系列講解(入門篇)使用Intent在Activity之間穿梭

    返回總目錄 上一篇: Android kotlin系列講解(入門篇)Activity的理解與基本用法 ???????你應該已經(jīng)對創(chuàng)建 Activity 的流程比較熟悉了,那我現(xiàn)在在 ActivityTest 項目再快速地創(chuàng)建一個 Activity 。 ???????還是右擊 com.example.activitytest 包→ New → Activity → Empty Views Activity ,會彈出

    2024年02月12日
    瀏覽(25)
  • ERC-3525 開發(fā)入門指南

    ERC-3525 開發(fā)入門指南

    ERC-3525 標準是以太坊社區(qū)批準通過的半勻質(zhì)化通證(Semifungible Token, 亦稱為半同質(zhì)化通證,簡稱 SFT)標準,由 Solv Protocol 提出。 ERC-3525 標準定義了一類新型的數(shù)字資產(chǎn),具有以下突出優(yōu)勢: 與 ERC-721 標準兼容,具有唯一 ID 和可視化外觀,可復用現(xiàn)有的大量 NFT 基礎(chǔ)設(shè)施; 可

    2023年04月16日
    瀏覽(18)
  • 【Android 從入門到出門】第一章:Android開發(fā)技能入門指南

    【Android 從入門到出門】第一章:Android開發(fā)技能入門指南

    ???♂? 個人主頁:@艾迦洼的個人主頁 ???作者簡介:后端程序猿 ?? 希望大家多多支持,如果文章對你有幫助的話,歡迎 ???????? 目錄 ?? 第一章:Android開發(fā)技能入門指南 ?? 1. 技術(shù)要求 ?? 2. 使用變量和習慣用法在Kotlin中編寫第一個程序 ?? 2.1 準備 ??

    2024年02月06日
    瀏覽(19)
  • 前端桌面應用開發(fā)實踐:Electron入門指南

    隨著互聯(lián)網(wǎng)的快速發(fā)展,前端開發(fā)不再局限于網(wǎng)頁應用,而是逐漸涉及到桌面應用的開發(fā)。Electron作為一種流行的前端桌面應用開發(fā)框架,為開發(fā)者提供了一種快速構(gòu)建跨平臺桌面應用的方式。本文將介紹Electron的基本概念和使用方法,并通過一個簡單的示例來說明其開發(fā)實踐

    2024年02月11日
    瀏覽(30)
  • HarmonyOS云開發(fā)基礎(chǔ)認證題目記錄——包括第一期:Serverless基礎(chǔ)、第二期:快速構(gòu)建用戶認證系統(tǒng)、第三期:云函數(shù)入門指南、第四期:云數(shù)據(jù)庫入門指南、第五期:云存儲入門指南。

    1. 【判斷題】? 應用架構(gòu)的演進依次經(jīng)歷了微服務架構(gòu)、單體架構(gòu)、Serverless架構(gòu)等階段。 錯誤 2. 【判斷題】? 認證服務手機號碼登錄需要填寫國家碼。 正確 3. 【判斷題】? 認證服務在綁定微信賬號后就不能再綁定QQ賬號了。 錯誤 4. 【判斷題】? 云函數(shù)可以根據(jù)函數(shù)的實際

    2024年02月05日
    瀏覽(133)
  • 【Rust指南】快速入門|開發(fā)環(huán)境|hello world

    【Rust指南】快速入門|開發(fā)環(huán)境|hello world

    ??本篇博客是Rust語言系列的開篇之作,以后有關(guān)Rust語言的文章也都會收錄在我的 《進軍Rust》 專欄里,我會精心打造這個專欄,幫助大家快速入門Rust語言,享受Rust語言帶來的編程樂趣。雖然Rust相比其他語言入門慢,但這恰巧說明了Rust語言的特色——安全高效。對Rust語言

    2024年02月15日
    瀏覽(19)
  • PyCharm入門級使用指南,Python開發(fā)必備!

    PyCharm是一個專業(yè)的Python IDE,可以提供全面的Python開發(fā)支持,包括代碼編輯、調(diào)試、測試、版本控制等功能。對于初學者來說,PyCharm可能會有點嚇人,但是隨著您的熟練使用,會發(fā)現(xiàn)它是一個非常強大且易于使用的工具。在本文中,我將向您介紹PyCharm的一些入門級使用方法。

    2024年02月16日
    瀏覽(13)
  • 熱門Java開發(fā)工具IDEA入門指南——插件安裝方式

    熱門Java開發(fā)工具IDEA入門指南——插件安裝方式

    IntelliJ IDEA,是java編程語言開發(fā)的集成環(huán)境。IntelliJ在業(yè)界被公認為最好的java開發(fā)工具,尤其在智能代碼助手、代碼自動提示、重構(gòu)、JavaEE支持、各類版本工具(git、svn等)、JUnit、CVS整合、代碼分析、 創(chuàng)新的GUI設(shè)計等方面的功能是非常強大的。 本文給大家講解在使用IntelliJ I

    2024年02月09日
    瀏覽(90)
  • 如何入門微信小程序開發(fā),超詳細學習指南大全

    2017年微信小程序發(fā)布開始,我就開始接觸和學習微信小程序,看著小程序不斷的更新迭代,功能越來越豐富,生態(tài)也越來越健全完善。 在這過程中,開發(fā)過商城小程序、停車掃碼計費小程序、工具打卡小程序、流量主小程序等等。 很多人問我小程序怎么開發(fā),一個人怎么制

    2024年02月09日
    瀏覽(17)
  • C++界面開發(fā)框架Qt 6.x入門指南 - 擁有程序主窗口

    C++界面開發(fā)框架Qt 6.x入門指南 - 擁有程序主窗口

    Qt技術(shù)交流群:166830288??????歡迎一起進群討論 點擊獲取Qt組件下載 Qt?Widget 是桌面環(huán)境中典型的用戶界面元素,這些小部件很好地集成到底層平臺,在 Windows、Linux 和 macOS 上提供原生外觀。 這些小部件成熟且具有豐富的用戶界面元素,適用于大多數(shù)傳統(tǒng)用戶界面。 與?

    2024年02月05日
    瀏覽(15)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包