国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Android開發(fā)學(xué)習(xí)教程(4)

2023-01-26 15:19 作者:ChatGPT云炬學(xué)長  | 我要投稿

Activity是什么

Activity就是你打開APP后所看到的各個界面,每個界面都是一個Activity。

Activity有什么用

APP通過Activity展示各個界面,處理和用戶的交互,比如文本框輸入內(nèi)容、點擊按鈕觸發(fā)事件、觀看視頻等等。

Activity的使用

本篇還是以第二篇中創(chuàng)建的Hello World項目為例,鼠標右鍵com.example.myapplication -> New -> Activity -> Empty Activity


輸入類的名稱,建議命名規(guī)則是功能+Activity,比如輸入TestActivity,我們可以看到,當我們輸入TestActivity時,下面的Layout Name會自動跟隨我們的輸入的類名而改變,這是因為Android Studio建議我們布局文件的名稱和Activity的名稱保持一致,方便我們可以直接在res目錄下查看每個Activity對應(yīng)的layout布局文件,而不用每次打開每個Activity然后通過setContentView去查找對應(yīng)的layout布局文件。輸入完之后點Finish

我們來看看點Finish之后Android Studio自動幫我們做了什么事情。

1. 自動在AndroidManifest.xml配置文件中生成TestActivity聲明,如下:

1
2
3
<activity
????android:name=".TestActivity"
????android:exported="false"?/>

2. 自動生成繼承自AppCompatActivity 的TestActivity并且調(diào)用setContentView,如下:

1
2
3
4
5
6
7
8
9
10
11
12
import?androidx.appcompat.app.AppCompatActivity;
import?android.os.Bundle;
public?class?TestActivity?extends?AppCompatActivity {
????@Override
????protected?void?onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_test);
????}
}

3. 自動創(chuàng)建一個activity_test.xml布局文件,如下:

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?encoding="utf-8"?>
<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=".TestActivity">
</androidx.constraintlayout.widget.ConstraintLayout>

4. 自動依賴用到的第三方庫,如下:

1
2
3
implementation?'androidx.appcompat:appcompat:1.1.0'
implementation?'com.google.android.material:material:1.0.0'
implementation?'androidx.constraintlayout:constraintlayout:1.1.3'

如果想看到TestActivity界面(活動),我們只需把AndroidManifest.xml中的MainActivity和TestActivity互換,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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/AppTheme">
????<activity
????????android:name=".MainActivity"
????????android:exported="false"?/>
????<activity
????????android:name=".TestActivity"
????????android:exported="true"
????????android:label="@string/app_name"
????????android:screenOrientation="portrait">
????????<intent-filter>
????????????<action?android:name="android.intent.action.MAIN"?/>
????????????<category?android:name="android.intent.category.LAUNCHER"?/>
????????</intent-filter>
????</activity>
</application>

然后點擊綠色三角形Run圖標運行即可

運行之后看到的是空白的頁面?那就對了。因為我們的布局文件只有一個布局,而沒有任何控件,下面我們加入一個顯示Hello Activity的標簽(暫時先跳過android:layout_xxxx、app:layout_xxxxx這些屬性的含義,下一篇章會講到),如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml?version="1.0"?encoding="utf-8"?>
<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=".TestActivity">
????<TextView
????????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
????????android:text="Hello Activity"
????????app:layout_constraintBottom_toBottomOf="parent"
????????app:layout_constraintEnd_toEndOf="parent"
????????app:layout_constraintStart_toStartOf="parent"
????????app:layout_constraintTop_toTopOf="parent"?/>
</androidx.constraintlayout.widget.ConstraintLayout>

再次運行,是不是可以看到Hello Activity了

?

源碼鏈接:https://yunjunet.cn/876707.html

Android開發(fā)學(xué)習(xí)教程(4)的評論 (共 條)

分享到微博請遵守國家法律
清流县| 临潭县| 板桥市| 桐梓县| 青冈县| 上杭县| 广南县| 威信县| 琼海市| 玉树县| 天长市| 莱阳市| 行唐县| 视频| 理塘县| 东山县| 昔阳县| 历史| 东台市| 文登市| 墨玉县| 汉阴县| 且末县| 静海县| 舒兰市| 措美县| 唐山市| 桑植县| 梁山县| 南昌市| 洛南县| 虎林市| 孝感市| 昌邑市| 惠水县| 建始县| 萍乡市| 佛冈县| 宁海县| 荆门市| 噶尔县|