【Android】安卓四大组件之Activity(一)
前言
Activity是Android学习中的一个重要组件,想要对其进行系统的了解可以分为几块内容,这一大章节的内容是有关于activity之间的页面跳转和数据传递,之后还有activity中的生命周期讲解。
1、认识AndroidManifest.xml
一个Manifest.xml
最外层用manifest
标签包裹,下面可以是application
,当然我们之前也学过uses-permission
,可以与application
同级
application
中设置属性,allowBackup
是允许备份,icon
是app图标,label
是app的名字,roundIcon
是圆角app图标,supportsRtl
是声明你的application是否愿意支持从右到左(原来RTL就是right-to-left 的缩写…)的布局。theme
当然就是主题啦!
Rtl到底是个啥玩意?有没有在QQ中见过使用阿拉伯语的,使用这些语言都是从右向左读的,所以需要从右向左适配。就是这样!
activity
就是这次学习的重点,是一个活动,也可以当作不同的活跃页面(大致理解),我们app中不同的界面就可以通过activity之间的跳转实现!
至于intent-filter
,就是一个意图过滤器,其中的action就是activity进行的一个action,而category可以理解为这个意图的类别,我们APP的主界面
的category就是LAUNCHER
2、如何实现activity之间的跳转?
我们可以使用startActivity()
方法或者startActivityForResult()
方法来跳转页面,这一小节就只讲startActivity
与intent
配合使用进行数据单项传递,在第4小节回讲到如何使用startActivityForResult
进行数据回传。
2.1 通过显式Intent来跳转同一个APP中的activity
显式Intent:按照名称(完全限定了类名)来指定要启动的组件。
可以看到下面的xml中注册了两个activity分别对应两个activity类
<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.Hello">
<activity
android:name=".QQLoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".QQLoginSuccessActivity"/>
</application>
需要注意的是,创建显式Intent启动Activity,系统将立刻启动Intent对象中指定的应用组件。
那么我来实际操作一个例子看看显式Intent如何跳转!
我们写一个登录页面的跳转测试用例,如果登录成功,就跳转到另一个activity页面中显示登录的信息!
第一个页面:登录界面xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@mipmap/bk">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:padding="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher_round"
android:text="QQ"
android:textSize="40sp"/>
<EditText
android:id="@+id/et_account"
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:hint="QQ号/手机号/邮箱"/>
<EditText
android:password="true"
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="密码"
android:textColor="@color/white"
tools:ignore="Deprecated" />
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textSize="20sp"/>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码"
android:textSize="16dp"
android:textColor="#87C6F8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="新用户注册"
android:textSize="16dp"
android:textColor="#87C6F8"/>
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="登录即代表阅读并同意服务条款"
android:textColor="#CCCFCF"
android:textSize="20sp" />
</RelativeLayout>
对应的activity java类
package top.woodwhale.hello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;