Activity(页面)是一个应用程序组件,提供一个屏幕用于交互,所有的任务都是在Activity上完成的,主Activity在AndroidManifest.xml里是这样出现的:
1 2 3 4 5 6 7 8
| <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
|
创建新的Activity
完整的Activity创建包括三个步骤:
- 在Layout目录下创建xml文件
- 创建与xml文件对应的java代码
- 在AndroidManifest.xml中注册页面配置
但实际上,在现在的Android Studio中,只需要右键项目软件包,然后选择”新建“-”Activity”-“Empty Views Activity”就可以了,Android Studio会做好一切的
活动页面显示与逻辑处理
Android应用利用XML标记和描绘应用界面,并使用Java(Kotlin)代码编写程序逻辑
对于任意一个Activity.java,都有一个activity.xml与之对应
我们创建一个空的MainActivity,Android Studio也会自动生成一个activity_main.xml
Android Studio生成的xml可能是这样的:
1 2 3 4 5 6 7 8 9 10
| <?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
|
当然,我们可以删除它提供的布局,并自己定义布局(以线性布局为例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world" />
</LinearLayout>
|
需要说明的是,定义 android:text="hello world" 时,会跳出警告,这是因为在实际开发中,这种赋值类型的语句都推荐将设置的值(比如这里的text文字,也包括后文设置的Button文字)存放在 app/res/values/strings.xml 中,并使用 @string/<name> 的方式进行调用,这些值在 string.xml 中存放的形式如下:
1 2 3 4 5
| <resources> <string name="app_name">My Application1</string> <string name="how_are_you">how are you?</string> <string name="skip_to">skip to...</string> </resources>
|
这个时候修改MainActivity.java,添加操作部件TextView的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.example.myapplication1;
import android.os.Bundle; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = findViewById(R.id.textView); tv.setText("nihao shijie!"); } }
|
调试app,就能看到居中的”nihao shijie!“的字样了
总结一下:
- Activity使用activity.xml描述页面布局
- 在Activity.java中可以使用setContentView来设置显示的内容
- 可以使用findViewById寻找到需要操作的组件
Activity的跳转
假设我们需要使用按钮从Activity1跳转到Activity2,那么我们首先要在Activity1中添加按钮:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">
<Button android:id="@+id/skipButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="skip to..."/>
</LinearLayout>
|
然后再去源文件Activity1.java中设置事件处理setOnClickListener(就是按下按钮后要做什么),并使用Intent(意图)对象和setClass方法来实现跳转
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
| package com.example.myapplication1;
import android.os.Bundle; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { Button button = findViewById(R.id.skipButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, MainActivity2.class); startActivity(intent); } }); } }
|
运行测试,就能看到页面上添加的“skip to…”按钮,点击它,就能跳转到Activity2了