一、前言
安卓应用往往有多个Activity,如何从一个Activity跳转到另一 个Activity呢? 往往要进行事件处理,并且
还会用到安卓里面一个十分重要的组件Intent, 可以把它看成连接安卓不同组件之间的桥梁。
二、笔记2.3概述
(一)三个基本控件
1、标签控件(TextView)
类层次继承图
1、使用显示意图启动组件
属性 | 含义 |
---|---|
text | 文本内容 |
text Size | 文本字号,单位:sp |
textColor | 文本颜色,#ff000-红色 |
layout_height | 高度,单位:dp(wrap_contenr. match_parent) |
layout_weight | 宽度,单位:dp(wrap_contenr. match_parent) |
设定内容居中、居左、居右
使用:--------- :居中
使用:---------:居左
使用:---------:居右
常用属性: text、textSize、 textColor.
2、编辑框控件(EditText)
类层次继承图:
常用属性: text, textSize. textColor. hint ..
属性 | 含义 |
---|---|
text | 文本内容 |
text Size | 文本字号,单位:sp |
textColor | 文本颜色,#ff000-红色 |
hint | 提示信息 |
singleLine | 单行(true or faise) |
layout_height | 高度,单位:dp(wrap_contenr. match_parent) |
layout_weight | 宽度,单位:dp(wrap_contenr. match_parent) |
3、按钮控件(Button)
类层次继承图|:
常用属性:
属性 | 含义 |
---|---|
text | 文本内容 |
text Size | 文本字号,单位:sp |
textColor | 文本颜色,#ff000-红色 |
hint | 提示信息 |
singleLine | 单行(true or faise) |
bacground | 背景颜色或背景图片 |
layout_height | 高度,单位:dp(wrap_contenr. match_parent) |
layout_weight | 宽度,单位:dp(wrap_contenr. match_parent) |
- EditText
与Button
是兄弟关系,它们的爹是TextView
(二)安卓事件处理机制
1.安卓事件处理概述
- 不论是桌面应用还是手机应用程序,需要对用户的动作提供响应,这种为用户动作提供响应的机制就是事件处理。
- 安卓提供了两种事件处理机制:基于回调的事件处理和基于监听的事件处理。
- 基于监听的事件处理是一种” 面向对象”的事件处理,涉及三种对象:事件源(
EventSource
)、事件(Event
) 、事件监听器 (EventListener
) 。
2、安卓事件处理步骤
- 以按钮单击事件处理为例说明安卓事件处理步骤
序号 | 任务 |
---|---|
1 | 在界面类里声明按钮控件变量 |
2 | 通过findViewByld()方法得到按钮实例 |
3 | 利用setOnClickListener()方法给按钮注册单击事件监听器 |
4 | 实现单击事件监听器接口,采用匿名实现方式 |
5 | 在接口的抽象方法里编写事件处理代码 |
(三) 案例演示:实现用户登录功能
1、创建安卓应用[UserLogin
]
- 基于
Empty Actvty
模板创建安卓应用
- 配置项目信息
- 单击【
Finish
】按钮
2、准备背景图片
- 将背景图片
background.png
拷贝到drawable
目录
3、基于模板创建登录窗口
- -基于
Empty Activity
模板创建LoginActvity
,要生成对应的布局文件,并且要设置为启动Activiy
- 单击【
Finish
】按钮
4、登录窗口布局资源文件 - 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:background="@drawable/background"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp"
tools:context=".LoginActivity">
<TextView
android:id="@+id/tv_user_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_login"
android:textColor="#0000ff"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textColor=