【Android】安卓四大组件之Activity(一)

【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

image-20220115211002836

2、如何实现activity之间的跳转?

我们可以使用startActivity()方法或者startActivityForResult()方法来跳转页面,这一小节就只讲startActivityintent配合使用进行数据单项传递,在第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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值