安卓讲课笔记5.2 编辑框

news2025/7/7 14:06:19

文章目录

  • 零、本讲学习目标
  • 一、导入新课
  • 二、新课讲解
    • (一)继承关系图
    • (二)编辑框常用属性
    • (三)教学案例:用户注册
      • 1、创建安卓应用
      • 2、准备图片素材
      • 3、主界面与主布局资源文件更名
      • 4、创建息界面类
      • 5、字符串资源文件
      • 6、注册界面布局资源文件
      • 7、信息界面布局资源文件
      • 8、用户注册界面类实现功能
      • 9、注册信息界面类实现功能
      • 10、启动应用,查看效果
  • 三、归纳总结
  • 四、上机操作

零、本讲学习目标

  1. 熟悉编辑框常用属性
  2. 能在应用中使用编辑框

一、导入新课

  • 在安卓应用中经常需要用户输入信息,比如用户名、密码、电话号码之类,一般采用编辑框来接收用户输入的任何文本信息。

二、新课讲解

在这里插入图片描述

(一)继承关系图

  • EditText是TextView的子类,用于接收用户输入的数据
    在这里插入图片描述
  • 课后大家可以去玩一玩EditText的子类AutoCompleteTextView

(二)编辑框常用属性

属性含义
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输入类型(普通文本、密码、邮件……)
maxLines最大行数
lines行数

(三)教学案例:用户注册

1、创建安卓应用

  • 基于Empty Activity模板创建安卓应用 - UserRegistration
    在这里插入图片描述
  • 单击【Finish】按钮
    在这里插入图片描述

2、准备图片素材

  • 将两张背景图片拷贝到drawable目录
    在这里插入图片描述

3、主界面与主布局资源文件更名

  • 将主界面类MainActivity更名为注册界面类RegistrationActivity
  • 将主布局资源文件activity_main.xml更名为注册布局资源文件activity_registration.xml
    在这里插入图片描述

4、创建息界面类

  • 基于Empty Activity模板创建信息界面类 - InformationActivity
    在这里插入图片描述
  • 单击【Finish】按钮
    在这里插入图片描述

5、字符串资源文件

  • 字符串资源文件 - strings.xml
    在这里插入图片描述
<resources>
    <string name="app_name">用户注册</string>
    <string name="name">姓名:</string>
    <string name="gender">性别:</string>
    <string name="age">年龄:</string>
    <string name="phone">电话:</string>
    <string name="email">邮箱:</string>
    <string name="home_page">主页:</string>
    <string name="memo">备注:</string>
    <string name="register">注册</string>
    <string name="cancel">取消</string>
</resources>

6、注册界面布局资源文件

  • 注册界面布局资源文件 - activity_registration.xml
    在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/reg_bg"
    android:gravity="center"
    android:orientation="vertical">

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

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_gender"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/age"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/phone"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="phone"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/email"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_email"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_home_page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/home_page"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_home_page"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textUri"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_memo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/memo"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_memo"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:lines="4" />
    </LinearLayout>

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

        <Button
            android:id="@+id/btn_register"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:onClick="doRegister"
            android:layout_marginRight="15dp"
            android:text="@string/register" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:onClick="doCancel"
            android:text="@string/cancel" />
    </LinearLayout>
</LinearLayout>
  • 查看预览效果
    在这里插入图片描述

7、信息界面布局资源文件

  • 信息界面布局资源文件 - activity_information.xml
    在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/info_bg"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:autoLink="phone"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:autoLink="email"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_home_page"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:autoLink="web"
        android:textColor="#0000ff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_memo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#0000ff"
        android:textSize="18sp" />
</LinearLayout>
  • 查看预览效果
    在这里插入图片描述

8、用户注册界面类实现功能

  • 用户注册界面类 - RegistrationActivity
    在这里插入图片描述
  • 声明变量
    在这里插入图片描述
  • 通过资源标识符获取控件实例
    在这里插入图片描述
  • 编写注册按钮单击事件处理方法
    在这里插入图片描述
  • 编写取消按钮单击事件处理方法
    在这里插入图片描述
  • 查看完整代码
package net.hw.registration;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class RegistrationActivity extends AppCompatActivity {

    private EditText etName;
    private EditText etGender;
    private EditText etAge;
    private EditText etPhone;
    private EditText etEmail;
    private EditText etHomePage;
    private EditText etMemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_registration);
        // 通过资源标识符获取控件实例
        etName = findViewById(R.id.et_name);
        etGender = findViewById(R.id.et_gender);
        etAge = findViewById(R.id.et_age);
        etPhone = findViewById(R.id.et_phone);
        etEmail = findViewById(R.id.et_email);
        etHomePage = findViewById(R.id.et_home_page);
        etMemo = findViewById(R.id.et_memo);
    }

    /**
     * 注册按钮单击事件处理方法
     *
     * @param view
     */
    public void doRegister(View view) {
        // 获取用户输入数据
        String name = etName.getText().toString();
        String gender = etGender.getText().toString();
        String age = etAge.getText().toString();
        String phone = etPhone.getText().toString();
        String email = etEmail.getText().toString();
        String homePage = etHomePage.getText().toString();
        String memo = etMemo.getText().toString();

        // 将各项输入数据打包
        Bundle data = new Bundle();
        data.putString("name", name);
        data.putString("gender", gender);
        data.putString("age", age);
        data.putString("phone", phone);
        data.putString("email", email);
        data.putString("home_page", homePage);
        data.putString("memo", memo);

        // 创建意图,指定起始组件与目标组件
        Intent intent = new Intent(this, InformationActivity.class);
        // 利用意图携带数据包
        intent.putExtras(data);
        // 按意图启动目标组件
        startActivity(intent);
    }

    /**
     * 取消按钮单击事件处理方法
     *
     * @param view
     */
    public void doCancel(View view) {
        finish(); // 关闭当前窗口
    }
}

9、注册信息界面类实现功能

  • 注册信息显示界面 - InformationActivity
    在这里插入图片描述
  • 声明变量
    在这里插入图片描述
  • 通过资源标识符获取控件实例
    在这里插入图片描述
  • 获取意图及其携带数据,用来设置标签内容
    在这里插入图片描述
  • 查看完整代码
package net.hw.registration;

import androidx.appcompat.app.AppCompatActivity;

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

public class InformationActivity extends AppCompatActivity {

    private TextView tvName;
    private TextView tvGender;
    private TextView tvAge;
    private TextView tvPhone;
    private TextView tvEmail;
    private TextView tvHomePage;
    private TextView tvMemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_information);

        // 通过资源标识符获取控件实例
        tvName = findViewById(R.id.tv_name);
        tvGender = findViewById(R.id.tv_gender);
        tvAge = findViewById(R.id.tv_age);
        tvPhone = findViewById(R.id.tv_phone);
        tvEmail = findViewById(R.id.tv_email);
        tvHomePage = findViewById(R.id.tv_home_page);
        tvMemo = findViewById(R.id.tv_memo);

        // 获取意图及其携带数据,用来设置标签内容
        Intent intent = getIntent();
        if (intent != null) {
            // 获取意图携带的数据包
            Bundle data = intent.getExtras();

            // 从数据包里按键取值
            String name = data.getString("name");
            String gender = data.getString("gender");
            String age = data.getString("age");
            String phone = data.getString("phone");
            String email = data.getString("email");
            String homePage = data.getString("home_page");
            String memo = data.getString("memo");

            // 设置各个标签的内容
            tvName.setText("姓名:" + name);
            tvGender.setText("性别:" + gender);
            tvAge.setText("年龄:" + age);
            tvPhone.setText("电话:" + phone);
            tvEmail.setText("邮箱:" + email);
            tvHomePage.setText("主页:" + homePage);
            tvMemo.setText("备注:" + memo);
        }
    }
}

10、启动应用,查看效果

  • 在注册界面输入数据
    在这里插入图片描述
  • 单击【注册】按钮,跳转到信息界面
    在这里插入图片描述
  • 当然可以测试电话、邮箱和主页链接
    在这里插入图片描述

三、归纳总结

  • 回顾本节课所讲的内容,并通过提问的方式引导学生解答问题并给予指导。

四、上机操作

  • 形式:单独完成
  • 题目:编写乘法运算程序
  • 要求:输入被乘数与乘数,单击【计算】按钮,显示结果。单击【清除】按钮,清空三个编辑框。单击【退出】按钮,退出应用程序。
    在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/4103.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据获取与预处理

文章目录Requests简介Requests库安装Requests库的基本操作Requests库的7个主要方法Request方法get方法Response对象的属性head方法post方法Requests简介 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 首先他是个第三方库&#xff0c;应用非常广泛 采用Apac…

《数据结构》(六)八大排序(下)

承接上篇的八大排序&#xff0c;今天本篇文章主要讲归并排序&#xff0c;冒泡排序&#xff0c;快速排序(挖坑&#xff0c;左右指针&#xff0c;前指针)和计数排序 八大排序交换排序冒泡排序冒泡排序思想代码冒泡排序总结快速排序快速排序思想三数取中快速排序之挖坑法挖坑法代码…

量化股票查询代码是什么?

量化股票查询代码是什么&#xff1f;接下来用一些代码来分析一下&#xff0c;如下&#xff1a; 做空95&#xff1a;HHV((HIGHLOWOPEN2*CLOSE)/5H-L,5),COLORBLUE;做空68: HHV((HIGH-LOWOPEN2*CLOSE)/5*2-L,5),COLORRED&#xff1b; 平衡点&#xff1a;LLV((HIGHLOWOPEN2*CLOSE…

狗厂员工来面试本想难为一下,问他内存溢出,结果被虐得连console.log也不敢写了

这次说到的面试题是关于node服务端内存溢出的问题&#xff0c;狗厂员工来面试本想难为一下&#xff0c;现在我连console.log也不敢写了 关于这道node内存溢出的问题&#xff0c;大哥从以下几个方面讲的&#xff0c;讲完我觉得自己得到了升华&#xff0c;现在搞得连代码也快不敢…

AI人脸检测/安全帽检测智能分析网关告警消息配置——微信告警消息配置

AI智能分析网关内置多种深度学习算法&#xff0c;可支持对接入的多路视频流进行智能检测、智能识别等&#xff0c;包括人脸检测与识别、车辆检测与识别、车牌识别、烟火识别、安全帽识别、区域入侵检测等。将智能分析网关与EasyCVR视频融合平台联合使用&#xff0c;可实现智能告…

Linux命令从入门到实战 ---- 用户管理命令

文章目录useradd添加新用户passwd设置用户密码id查看用户是否存在查看创建了哪些用户su切换用户userdel删除用户who查看登录用户信息sudo设置普通用户具有root权限用户组groupadd 新增用户组usermod修改用户groupdel删除用户组groupmod修改用户组总结useradd添加新用户 将usera…

安卓学习笔记5.3 按钮、图像视图与图像按钮

文章目录零、本讲学习目标一、导入新课二、新课讲解&#xff08;一&#xff09;按钮控件1、继承关系图2、常用属性&#xff08;二&#xff09;图像视图1、继承关系图2、常用属性&#xff08;三&#xff09;图像按钮1、继承关系图2、常用属性&#xff08;四&#xff09;教学案例…

vue无需改动代码的SEO【百度爬取】优化--puppeteer(详细流程)

vue无需改动代码的SEO优化–puppeteer&#xff08;详细流程&#xff09; 目录vue无需改动代码的SEO优化--puppeteer&#xff08;详细流程&#xff09;一级目录二级目录三级目录一、安装puppeteer&#xff1a;npm install puppeteer --save安装依赖二、编写puppeteer服务js文件p…

DDD领域驱动设计基础

什么领域驱动模型 领域驱动模型一种设计思想&#xff0c;我们又称为DDD设计思想。是一种为了解决传统设计思想带来的维护困难&#xff0c;沟通困难和交互困难而产生的一种新的思想。 架构模式的演进 单体架构 采用面向对象的设计方法&#xff0c;系统包括业务接入层、业务逻…

Eclipse切JRE环境后如何恢复- Unrecognized option: --enable-preview

场景 使用switch 新特性 配合 lambda 练习小案例 // 需求&#xff1a; 1 2 3 -> 一、二、 三 int num 1; switch ( num) {// jdk13 可以缺省 break 并且 单语句可以省略 花括号 case 1 -> { System.out.println("一"); }case 2 -> System.out.p…

[附源码]计算机毕业设计JAVAjsp宠物店管理系统

[附源码]计算机毕业设计JAVAjsp宠物店管理系统 项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybat…

Mybatis-Plus知识点[MyBatis+MyBatis-Plus的基础运用]

目录 前言 一、了解Mybatis-Plus 1.简介 2.Mybatis-Plus具有的特性 3.支持数据库 4.框架结构 5.官网链接 二、快速开始 2.1.创建数据库以及表 2.2.创建工程 2.3 MybatisMybatis-Plus的使用 2.3.1创建一个itcast-mybatis-plus-simple的maven项目 2.3.2写UserMapper接口 2.3.3写U…

北京化工大学数据结构2022/11/3作业 题解

目录 问题 A: 二叉树非递归前序遍历-附加代码模式 问题 B: 二叉树非递归中序遍历-附加代码模式 问题 C: 二叉树非递归后序遍历-附加代码模式 问题 D: 求二叉树中序遍历序根节点的下标 问题 E: 根据前序中序还原二叉树 问题 F: 算法6-12&#xff1a;自底向上的赫夫曼编码 …

ServletConfig和ServletContext接口

一、ServletConfig接口详解 1、简介 Servlet 容器初始化 Servlet 时&#xff0c;会为这个 Servlet 创建一个 ServletConfig 对象&#xff0c;并将 ServletConfig 对象作为参数传递给 Servlet 。通过 ServletConfig 对象即可获得当前 Servlet 的初始化参数信息。一个 Web 应用中…

微电网优化调度(风、光、储能、柴油机)(Python代码实现)

&#x1f4a5;&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️❤️&#x1f4a5;&#x1f4a5;&#x1f4a5; ​ &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻…

【Linux命令】文件和目录权限

【Linux命令】文件和目录权限 权限查看 众所周知&#xff0c;可以使用 ls -l 来查看文件和目录的详细信息&#xff0c;那么输出的东西是什么呢&#xff1f; 我们先来看 文件类型&#xff1a; -&#xff1a;普通文件&#xff1b;d&#xff1a;目录文件&#xff1b;b&#xff…

网络协议:TCP三次握手与四次挥手

本篇内容包括&#xff1a;TCP/IP 传输协议&#xff08;TCP/IP 传输协议简介&#xff0c;IP 协议&#xff0c;UDP 协议&#xff0c;TCP 协议介绍&#xff09;&#xff0c;TCP 的三次握手、TCP 的四次挥手 以及 TCP 协议是怎么保证有效传输等内容。 一、TCP/IP 传输协议 1、TCP/…

【仿牛客网笔记】 Redis,一站式高性能存储方案——Redis入门

Redis可以开发对性能要求较高的功能。还可以利用Redis重构我们现有的功能。 NoSQL关系型数据库之外的统称。 快照有称为RDB 以快照的形式 不适合实时的去做&#xff0c;适合一段时间做一次。 日志又称AOF 以日志的形式每执行一次就存入到硬盘中&#xff0c;可以做到实时的存储以…

JAVA外卖订餐系统毕业设计 开题报告

本文给出的java毕业设计开题报告&#xff0c;仅供参考&#xff01;&#xff08;具体模板和要求按照自己学校给的要求修改&#xff09; 选题目的和意义 目的&#xff1a;本课题主要目标是设计并能够实现一个基于java的外卖点菜系统&#xff0c;管理员通过后台添加菜品&#xf…

卷积神经网络CNN

卷积神经网络CNN CNN通常用于影像处理 为什么需要CNN 为什么需要CNN&#xff0c;我用普通的fully connected的反向传播网络进行图像训练会怎样 需要过多参数 假设一张彩色的图为100100的&#xff0c;那么像素点就是1001003&#xff0c;那么输入层为三万维 假设下一层隐含层有…