安卓学习Day02(事件处理、窗口跳转与传递数据)

学习目标

  • 会使用线性布局和三个基本控件设计用户界面
  • 能说出安卓事件处理机制的实现步骤
  • 能编写代码完成安卓应用的事件处理
  • 会使用Intent控件实现窗口的跳转
  • 会在不同的窗口之间传递数据(Bundle)

案例演示:实现用户登录功能

在这里插入图片描述

步骤一:创建安卓应用(UserLogin)

在这里插入图片描述

步骤二:基于模板创建登录窗口LoginActivity

在这里插入图片描述
在这里插入图片描述

步骤三:准备图片资源

在这里插入图片描述

步骤四:创建按钮边框配置文件

  • 在drawable目录里创建button.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#0a0934" />
    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
    <stroke
        android:width="0.5dp"
        android:color="#787878" />
</shape>

步骤五:在字符资源文件里定义变量

  • 打开strings.xml文件
<resources>
    <string name="app_name">用户登录</string>
    <string name="username_hint">输入用户名</string>
    <string name="password_hint">输入密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>


步骤六:编写登录窗口布局资源文件(activity_login.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="2">
        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:src="@mipmap/user"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/users"/>

            <EditText
                android:id="@+id/edt_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/username_hint"
                android:singleLine="true"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/password"/>

            <EditText
                android:id="@+id/edt_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password_hint"
                android:singleLine="true"
                android:inputType="textPassword"/>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_horizontal"
        android:layout_weight="1">

        <Button
            android:id="@+id/btn_login"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:textSize="20sp"
            android:background="@drawable/button"
            android:layout_marginRight="10dp"/>

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textSize="20sp"
            android:background="@drawable/button"/>
    </LinearLayout>
</LinearLayout>

步骤七:编写主窗口布局资源文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textColor="#0000ff"/>

</LinearLayout>

步骤八:编写项目清单文件AndroidManifest.xml

  • 删除MainActivity元素的意图过滤器
    在这里插入图片描述
    在这里插入图片描述

步骤九:编写登录窗口(LoginActivity)

1)声明控件变量

在这里插入图片描述

2)通过资源标识符获取控件实例

在这里插入图片描述

3)登录按钮事件处理
  • 给登录按钮注册监听器,实现监听器接口,编写事件处理方法
    在这里插入图片描述
4)取消按钮事件处理
  • 给取消按钮注册监听器,实现监听器接口,编写事件处理方法
    在这里插入图片描述

步骤十:修改主界面MainActivity

在这里插入图片描述

测试结果

  • 用户名和密码正确的结果
    在这里插入图片描述
  • 用户名或密码错误的结果
    在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值