创建空Activity

修改默认build.gradle

build.gradle 外部依赖

引入阿里云maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
buildscript {
repositories {
// 阿里云maven

maven{ url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {

// 阿里云maven

maven{ url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

app/build.gradle

最小sdk用16,小于16则需要手动建立程序代码与布局,并手动建立代码与布局之间的关系

1
2
3
4
5
6
7
8
9
defaultConfig {
applicationId "com.example.activitytest"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

建好后,重新编译,并sync代码

upload successful

upload successful

创建布局

建立布局目录

app/src/main/res,右键建立布局目录,目录名称layout

upload successful

建立布局文件

app/src/main/res/layout,右键建立布局文件

upload successful

创建Activity代码

点击app/src/main/java/程序目录

右键新增,EmptyActivity,输入活动名称

upload successful

sdk小于16,需要点击Gallery,选择。否则点击Empty Ativity。

如果是系统建立的活动名称,各种包以及引入,SDK<16的情况,则需要的手工引入

引入的包

1
2
3
4
5
6
7
8
9
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

默认构造类,需要继承AppCompatActivity类

1
2
3
4
5
6
7
public class FirstActivity extends AppCompatActivity {
// 创建默认的活动,并在活动关闭后保存状态
super.onCreate(savedInstanceState);

// 设置默认布局图
setContentView(R.layout.first_layout);
}
修改配置文件

编辑AndroidManifest.xml

增加活动,增加Intent信息传递,设置该活动为主活动,并为默认活动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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/Theme.ActivityTest" >

<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

重新构建项目,运行APP