当前位置:   article > 正文

AndroidStudio2.3 窗口跳转与传递数据_android studio弹出吐司提示“密码错误请重试。

android studio弹出吐司提示“密码错误请重试。

一、学习目标

  • 掌握事件处理
  • 理解意图Intent
  • 掌握窗口跳转与传递数据

二、概括

  • 安卓应用往往有多个Activity,如何从一个Activity跳转到另一个Activity呢?往往要进行事件处理,并且还会用到安卓里面一个十分重要的组件——Intent,可以把它看成连接安卓不同组件之间的桥梁。

三、讲解

(一)三个基本控件

1、标签控件(TextView)

  • 常用属性
属性含义
text文本内容
textSize文本字号,单位:sp
textColor文本颜色,#ff0000 - 红色
layout_heigh高度,单位:dp (wrap_content, match_parent)
layout_weight宽度,单位:dp (wrap_content, match_parent)

2、编辑框控件(EditText)

  • 常用属性
属性含义
text文本内容
textSize文本字号,单位:sp
textColor文本颜色,#ff0000 - 红色
hint提示信息
singleLine单行(true or false)
layout_height高度,单位:dp (wrap_content, match_parent)
layout_weight宽度,单位:dp (wrap_content, match_parent)
inputType输入类型(普通文本、密码、邮件……)

3、按钮控件(Button)

  • 常用属性
属性含义
text文本内容
textSize文本字号,单位:sp
textColor文本颜色,#ff0000 - 红色
background背景颜色或背景图片
layout_height高度,单位:dp (wrap_content, match_parent)
layout_weight宽度,单位:dp (wrap_content, match_parent)

在这里插入图片描述

  • EditText与Button是兄弟关系,它们的爹是TextView

(二)安卓事件处理机制

1、安卓事件处理概述

  • 不论是桌面应用还是手机应用程序,需要对用户的动作提供响应,这种为用户动作提供响应的机制就是事件处理。
  • 安卓提供了两种事件处理机制:基于回调的事件处理和基于监听的事件处理。
  • 基于监听的事件处理是一种“面向对象”的事件处理,涉及三种对象:事件源(EventSource)、事件(Event)、事件监听器(EventListener)。

2、安卓事件处理步骤

  • 以按钮单击事件处理为例说明安卓事件处理步骤
序号任务
1在界面类里声明按钮控件变量
2通过findViewById()方法得到按钮实例
3利用setOnClickListener()方法给按钮注册单击事件监听器
4实现单击事件监听器接口,采用匿名实现方式
5在接口的抽象方法里编写事件处理代码

(三)案例演示:实现用户登录

1、创建安卓应用

  • 基于Empty Activity模板创建安卓应用
    在这里插入图片描述
  • 配置项目信息
    在这里插入图片描述
  • 单击【Finish】按钮
    在这里插入图片描述

2、准备背景图片

  • 将背景图片background.png拷贝到drawable目录
    在这里插入图片描述

3、基于模板创建登录窗口

  • 基于Empty Activity模板创建LoginActivity,要生成对应的布局文件,并且要设置为启动Activity
    在这里插入图片描述
  • 单击【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="#000000"
           android:textSize="20sp" />

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

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:orientation="horizontal">

       <TextView
           android:id="@+id/tv_password"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/password"
           android:textColor="#000000"
           android:textSize="20sp" />

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

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:orientation="horizontal">

       <Button
           android:id="@+id/btn_login"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="20dp"
           android:paddingLeft="20dp"
           android:paddingRight="20dp"
           android:text="@string/login"
           android:textSize="20sp" />

       <Button
           android:id="@+id/btn_cancel"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="20dp"
           android:paddingRight="20dp"
           android:text="@string/cancel"
           android:textSize="20sp" />
   </LinearLayout>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
属性作用
gravit用于设置容器的子控件的对齐方式,或控件的内容的对齐方式
orientation线性布局的方向(horizontal——横向、vertical——纵向)
padding内边距,用于设置子控件与父容器边框的距离,或控件的内容与控件边框的距离;(paddingLeft、paddingRight、paddingTop、paddingBottom)
layout_margin外边距,用于设置控件之间的距离;(layout_marginLeft,layout_marginRight、layout_marginTop、layout_marginBottom)

5、主窗口布局资源文件

  • 主窗口布局资源文件 - activity_main.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"
    tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textColor="#0000ff"/>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

6、安卓项目清单文件

  • 安卓项目清单文件AndroidManifest.xml,删除MainAcivity元素包含的意图过滤器
    在这里插入图片描述

7、字符串资源文件

  • 在字符串资源文件strings.xml里定义所需字符串变量
    在这里插入图片描述
<resources>
    <string name="app_name">用户登录</string>
    <string name="user_login">用户登录</string>
    <string name="username">用户:</string>
    <string name="input_username">请输入用户名</string>
    <string name="password">密码:</string>
    <string name="input_password">请输入密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

8、登录窗口功能实现

  • 登录窗口 - LoginActivity
    在这里插入图片描述
(1)声明控件变量
  • 两个编辑框变量和两个按钮变量
    在这里插入图片描述
(2)通过资源标识符获取控件实例
  • 通过findViewById()方法获取控件实例(类似于JavaScript里的getElementById()方法)
    在这里插入图片描述
(3)登录按钮事件处理
  • 给登录按钮注册单击监听器,实现监听器接口,并且编写事件处理代码
  • 首先获取用户输入的用户名和密码,然后判断是否正确,弹出不同的吐司

在这里插入图片描述

(4)取消按钮事件处理
  • 给取消按钮注册单击监听器,实现监听器接口,并且编写事件处理代码
  • 单击取消按钮,关闭登录窗口(另外还有一种处理方法,只是清空两个编辑框而不关闭窗口)
    在这里插入图片描述

9、启动应用,查看效果

  • 思考:如果希望用户登录标签与下面的输入用户名编辑隔开一点,应该怎么设置标签的属性?
    在这里插入图片描述
(1)输入用户名与密码正确的情况
  • 用户名:fzy
  • 密码:512030
    在这里插入图片描述
(2)输入用户名或密码错误的情况
  • 用户名:mike
  • 密码:45678
    在这里插入图片描述
  • 可以指定两个编辑框的宽度,比如200dp
    在这里插入图片描述
    在这里插入图片描述
  • 重启应用,查看效果
    在这里插入图片描述

(四)案例演示:修改用户登录程序

  • 功能要求:当登录成功,跳转到主窗口,显示用户名与密码。

1、修改登录窗口代码

  • 创建显式意图,利用意图携带数据,按照意图启动目标组件
    在这里插入图片描述

2、修改主窗口代码

  • 接收登录窗口通过意图传递的数据并显示在标签里
    在这里插入图片描述

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);
        // 通过资源标识符获取控件实例
        tvMessage = findViewById(R.id.tv_message);
        // 获取意图
        Intent intent = getIntent();
        // 判断意图是否为空
        if (intent != null) {
            // 获取意图携带的数据
            String username = intent.getStringExtra("username");
            String password = intent.getStringExtra("password");
            // 拼接用户信息
            String message = "登录成功!\n用户:" + username + "\n密码:" + password;
            // 设置标签属性,显示用户信息
            tvMessage.setText(message);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

3、启动应用,查看效果

在这里插入图片描述

(1)输入用户名与密码正确的情况
  • 用户名:fzy
  • 密码:512030
    在这里插入图片描述
  • 单击【登录】按钮
    在这里插入图片描述
(2)输入用户名或密码错误的情况
  • 用户名:fzy
  • 密码:123456
    在这里插入图片描述

4、修改登录窗口代码

  • 将多项数据封装成数据包,通过意图传递数据包
    在这里插入图片描述

5、修改主窗口功能代码

  • 通过意图获取数据包,然后从数据包里按键取得各项数据
    在这里插入图片描述

6、启动应用,测试效果

  • 登录操作录屏
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/705004
推荐阅读
相关标签
  

闽ICP备14008679号