Android中Activity組件實(shí)例介紹
Activity 概述
在 Android 應(yīng)用中,提供了 4 大基本組件,分別是 Activity、Service、BroadcastReceiver 和 ContentProvider。而 Activity 是 Android 應(yīng)用最常見(jiàn)的組件之一。Activity 的中文意思是活動(dòng)。在 Android 中,Activity 代表手機(jī)或者平板電腦中的一屏,它提供了和用戶交互的可視化界面。在一個(gè) Activity 中,可以添加很多組件,這些組件負(fù)責(zé)具體的功能。
在一個(gè) Android 應(yīng)用中,可以有多個(gè) Activity。這些 Activity 組成了 Activity 棧(Stack),當(dāng)前活動(dòng)的 Activity 位于棧頂,之前的 Activity 被壓入下面,成為非活動(dòng) Activity,等待是否可能被恢復(fù)為活動(dòng)狀態(tài)。
啟動(dòng) Activity 的兩種情況
①、在一個(gè) Android 應(yīng)用中,只有一個(gè) Activity 時(shí),那么只需要在 AndroidManifest.xml 文件中對(duì)其進(jìn)行備注,并且將其設(shè)置為程序的入口。這樣,當(dāng)運(yùn)行時(shí),將自動(dòng)啟動(dòng)該 Activity。
②、在一個(gè) Android 應(yīng)用中,存在多個(gè) Activity 時(shí),需要應(yīng)用 startActivity() 方法來(lái)啟動(dòng)需要的 Activity。
關(guān)閉 Activity
在 Android 中,如果想要關(guān)閉當(dāng)前的 Activity,可以使用 Activity 類提供的 finish()方法。
舉例說(shuō)明:?jiǎn)?dòng)和關(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 對(duì)象
Intent intent = new Intent(MainActivity.this,PasswordActivity.class);
//啟動(dòng) 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="賬號(hào):"
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ī)號(hào)"
/>
<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="注冊(cè)"
/>
<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)建新活動(dò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ī)號(hào)"
/>
<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="請(qǐng)輸入郵箱或手機(jī)號(hào)"/>
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例
這篇文章主要介紹了Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例,講解了ContentProvider對(duì)系統(tǒng)中聯(lián)系人及多媒體資源的管理例子,需要的朋友可以參考下2016-04-04
Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2017-01-01
Android自定義實(shí)現(xiàn)可滑動(dòng)按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可滑動(dòng)的按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android調(diào)用系統(tǒng)圖庫(kù)獲取圖片的方法
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用系統(tǒng)圖庫(kù)獲取圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android init.rc文件詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android init.rc文件詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Flutter Http網(wǎng)絡(luò)請(qǐng)求實(shí)現(xiàn)詳解
這篇文章主要介紹了Flutter Http網(wǎng)絡(luò)請(qǐng)求實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Android使用fastjson庫(kù)解析json字符串實(shí)戰(zhàn)
fastjson是一個(gè)Java語(yǔ)言編寫的高性能功能完善的JSON庫(kù),它采用一種“假定有序快速匹配”的算法,把JSON?Parse的性能提升到極致,是目前Java語(yǔ)言中最快的JSON庫(kù),Fastjson接口簡(jiǎn)單易用,已經(jīng)被廣泛使用在緩存序列化、協(xié)議交互、Web輸出、Android客戶端等多種應(yīng)用場(chǎng)景2023-11-11
Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼
這篇文章主要介紹了Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

