Android中Activity組件實(shí)例介紹
Activity 概述
在 Android 應(yīng)用中,提供了 4 大基本組件,分別是 Activity、Service、BroadcastReceiver 和 ContentProvider。而 Activity 是 Android 應(yīng)用最常見的組件之一。Activity 的中文意思是活動。在 Android 中,Activity 代表手機(jī)或者平板電腦中的一屏,它提供了和用戶交互的可視化界面。在一個(gè) Activity 中,可以添加很多組件,這些組件負(fù)責(zé)具體的功能。
在一個(gè) Android 應(yīng)用中,可以有多個(gè) Activity。這些 Activity 組成了 Activity 棧(Stack),當(dāng)前活動的 Activity 位于棧頂,之前的 Activity 被壓入下面,成為非活動 Activity,等待是否可能被恢復(fù)為活動狀態(tài)。
啟動 Activity 的兩種情況
①、在一個(gè) Android 應(yīng)用中,只有一個(gè) Activity 時(shí),那么只需要在 AndroidManifest.xml 文件中對其進(jìn)行備注,并且將其設(shè)置為程序的入口。這樣,當(dāng)運(yùn)行時(shí),將自動啟動該 Activity。
②、在一個(gè) Android 應(yīng)用中,存在多個(gè) Activity 時(shí),需要應(yīng)用 startActivity() 方法來啟動需要的 Activity。
關(guān)閉 Activity
在 Android 中,如果想要關(guān)閉當(dāng)前的 Activity,可以使用 Activity 類提供的 finish()方法。
舉例說明:啟動和關(guān)閉 Activity
核心代碼如下
// MainActivity public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); TextView password = (TextView) findViewById(R.id.wang_mima); password.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創(chuàng)建 Intent 對象 Intent intent = new Intent(MainActivity.this,PasswordActivity.class); //啟動 PasswordActivity startActivity(intent); } }); } }
<?xml version="1.0" encoding="utf-8"?> <TableLayout 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:background="#CCCC99" android:stretchColumns="0,3"> <!-- 第一行 --> <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingTop="200dp" > <TextView /> <TextView android:text="賬號:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="15dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:hint="郵箱或手機(jī)號" /> <TextView/> </TableRow> <!-- 第二行 --> <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingTop="2dp"> <TextView /> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15dp" android:text="密碼" android:gravity="center_horizontal" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="輸入 6-16 位數(shù)字或字母" android:textSize="15dp" /> <TextView/> </TableRow> <!-- 第三行 --> <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content"> <TextView/> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="注冊" /> <Button android:layout_width="15dp" android:layout_height="wrap_content" android:text="登錄" /> <TextView/> </TableRow> <!-- 第四行 --> <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingTop="15dp" > <TextView /> <TextView /> <TextView android:id="@+id/wang_mima" android:text="忘記密碼?" android:textColor="#FF4500" android:gravity="right" /> </TableRow> </TableLayout>
所得 主界面
//創(chuàng)建新活動 PasswordActivity package com.example.example61; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; public class PasswordActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_password); //獲得布局文件中的關(guān)閉按鈕 ImageButton close = (ImageButton) findViewById(R.id.close); close.setOnClickListener(new View.OnClickListener(){ @Override //關(guān)閉當(dāng)前 Activity public void onClick(View v) { finish(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".PasswordActivity" android:background="#CCCC99"> <ImageButton android:id="@+id/close" android:layout_width="60dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:background="#0099CC" android:padding="5dp" android:scaleType="centerInside" android:src="@drawable/a" /> <TextView android:id="@+id/t1" android:layout_width="350dp" android:layout_height="40dp" android:layout_alignBottom="@+id/close" android:layout_alignParentRight="true" android:background="#0099CC" android:paddingHorizontal="120dp" android:text="找回密碼" android:textSize="25dp" /> <TextView android:id="@+id/textview" android:layout_below="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:textSize="25dp" android:text="郵箱或手機(jī)號" /> <EditText android:id="@+id/edittext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:hint="請輸入郵箱或手機(jī)號"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:background="#0099C" android:text="提交" /> </RelativeLayout>
單擊找回密碼所得界面
總結(jié)
到此這篇關(guān)于Android中Activity組件實(shí)例介紹的文章就介紹到這了,更多相關(guān)Android Activity組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的多種方法
本篇文章主要是介紹了Android開發(fā)之監(jiān)聽button點(diǎn)擊事件的方法,Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的方法總結(jié),有興趣的可以了解一下。2016-11-11深入解析Android中View創(chuàng)建的全過程
這篇文章主要給大家深入的解析了關(guān)于Android中View創(chuàng)建的全過程,文中介紹的非常詳細(xì),相信對大家會有一定的參考借鑒,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-03-03Android使用ContentProvider初始化SDK庫方案小結(jié)
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04android打開應(yīng)用所在的市場頁面進(jìn)行評分操作的方法
這篇文章主要介紹了android打開應(yīng)用所在的市場頁面進(jìn)行評分操作的方法,涉及Android操作市場頁面評分效果的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
這篇文章主要為大家詳細(xì)介紹了Android關(guān)于Glide的使用,內(nèi)容豐富,高斯模糊、加載監(jiān)聽、圓角圖片希望大家可以掌握,感興趣的小伙伴們可以參考一下2016-11-11Android中AOP的應(yīng)用實(shí)踐之過濾重復(fù)點(diǎn)擊
這篇文章主要給大家介紹了關(guān)于Android中AOP的應(yīng)用實(shí)踐之過濾重復(fù)點(diǎn)擊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04