欢迎光临散文网 会员登陆 & 注册

Android开发学习教程(4)

2023-01-26 15:19 作者:ChatGPT云炬学长  | 我要投稿

Activity是什么

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

Activity有什么用

APP通过Activity展示各个界面,处理和用户的交互,比如文本框输入内容、点击按钮触发事件、观看视频等等。

Activity的使用

本篇还是以第二篇中创建的Hello World项目为例,鼠标右键com.example.myapplication -> New -> Activity -> Empty Activity


输入类的名称,建议命名规则是功能+Activity,比如输入TestActivity,我们可以看到,当我们输入TestActivity时,下面的Layout Name会自动跟随我们的输入的类名而改变,这是因为Android Studio建议我们布局文件的名称和Activity的名称保持一致,方便我们可以直接在res目录下查看每个Activity对应的layout布局文件,而不用每次打开每个Activity然后通过setContentView去查找对应的layout布局文件。输入完之后点Finish

我们来看看点Finish之后Android Studio自动帮我们做了什么事情。

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

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

2. 自动生成继承自AppCompatActivity 的TestActivity并且调用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. 自动创建一个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开发学习教程(4)的评论 (共 条)

分享到微博请遵守国家法律