文章目录
学习目标
- 会使用线性布局和三个基本控件设计用户界面
- 能说出安卓事件处理机制的实现步骤
- 能编写代码完成安卓应用的事件处理
- 会使用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
测试结果
- 用户名和密码正确的结果
- 用户名或密码错误的结果