Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

这篇具有很好参考价值的文章主要介绍了Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Android学习笔记

疯狂Android讲义

第2章 Android 应用的界面编程

2.3 第2组 UI组件:TextView及其子类

“九层之台,起于累土”——无论看上去多么美观的UI界面,开始都是先创建容器(ViewGroup的实例),然后不断地向容器中添加界面组件,最后形成一个美观的UI 界面的。

2.3.1 文本框TextView和编辑框EditText的功能和用法

TextView直接继承了View,它还是EditText、Button两个UI组件类的父类。TextView的作用就是在界面上显示文本。

从功能上来看,TextView其实就是一个文本编辑器,只是Android关闭了它的文字编辑功能。如果开发者想要定义一个可编辑内容的文本框,则可以使用它的子类: EditText,EditText 允许用户编辑文本框中的内容。

TextView还派生了一个 CheckedTextView,CheckedTextView增加了一个 checked 状态,开发者可通过setChecked(boolean)和 isChecked()方法来改变、访问该组件的checked 状态。除此之外,该组件还可通过setCheckMarkDrawable()方法来设置它的勾选图标。

TextView及其子类的类图:

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

TextView和 EditText具有很多相似之处,它们之间的最大区别在于TextView不允许用户编辑文本内容,而 EditText则允许用户编辑文本内容。

TextView支持的XML属性及相关方法

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

实例——功能丰富的文本框

创建新项目

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

布局文件

<?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:orientation="vertical">

    <!--  设置字号为20pt,在文本框结尾处绘制图片  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableEnd="@mipmap/ic_launcher"
        android:text="我爱Android"
        android:textSize="20pt" />

    <!--  设置中间省略,所有字母大写  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我爱Java"
        android:textAllCaps="true"
        android:textSize="20sp" />

    <!--  为邮件、电话增加链接  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="email|phone"
        android:singleLine="true"
        android:text="邮件是111111111@163.com,电话是18700000000" />

    <!--  设置文字颜色、大小,并使用阴影  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shadowColor="#00f"
        android:shadowDx="10.0"
        android:shadowDy="8.0"
        android:shadowRadius="3.0"
        android:text="测试文字"
        android:textColor="#f00"
        android:textSize="18pt" />

    <CheckedTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="@drawable/ok"
        android:text="可勾选的文本" />

    <!--  通过background指定背景  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_border"
        android:text="带边框的文本"
        android:textSize="24pt" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_border2"
        android:text="圆角边框、渐变背景的文本"
        android:textSize="24pt" />

</LinearLayout>

bg_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- 设置背景色为透明色 -->
   <solid android:color="#0000"/>
   <!-- 设置红色边框 -->
   <stroke android:width="2dp" android:color="#f00" />
</shape>

bg_border2.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
   <!-- 指定圆角矩形的4个圆角的半径 -->
   <corners android:topLeftRadius="20dp"
      android:topRightRadius="10dp"
      android:bottomRightRadius="20dp"
      android:bottomLeftRadius="10dp"/>
   <!-- 指定边框线条的宽度和颜色 -->
   <stroke android:width="4px" android:color="#f0f" />
   <!-- 指定使用渐变背景色,使用sweep类型的渐变
   颜色从红色→绿色→蓝色 -->
   <gradient android:startColor="#f00"
      android:centerColor="#0f0"
      android:endColor="#00f"
      android:type="sweep"/>
</shape>

运行效果

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法

解释

  • 第1个TextView指定android;textSize=“20pt”,这就指定了字号为20pt。而且指定了在文本框的结尾处绘制图片。
  • 第2个TextView指定 android:ellipsize=“middle” ,这就指定了当文本多于文本框的宽度时,从中间省略文本。而且指定了android:textAllCaps=“true”,表明该文本框的所有字母大写。
  • 第3个TextView指定android:autoLink=“emailphone”,这就指定了该文本框会自动为文本框内的 E-mail地址、电话号码添加超链接。
  • 第4个TextView指定一系列 android:shadowXXX属性,这就为该文本框内的文本内容添加了阴影。
  • 第5个CheckedTextView指定android:checkMark=“@drawable/ok”,这就指定了该可勾选文本框的勾选图标。
  • 第6个TextView指定了背景,背景是由XML文件定义的,将该文件放在drawable文件夹内,该XML文件也可被当成 Drawable使用。
  • 第7个TextView使用 Drawable指定使用圆角边框、渐变背景。第二个文本框所指定的背景也是由XML 文件定义的,将该文件放在 drawable文件夹内,该XML 文件也可被当成Drawable使用。

这些属性同样适用于EditText和Button。

2.3.2 EditText的功能和用法

EditText 与 TextView的最大区别在于:EditText可以接受用户输入。

EditText组件最重要的属性是 inputType,该属性相当于HTML 的<input>元素的type属性,用于将EditText设置为指定类型的输入组件。inputType能接受的属性值非常丰富,而且随着Android版本的升级,该属性能接受的类型还会增加。

EditText 还派生了如下两个子类:

  • AutoCompleteTextView:带有自动完成功能的 EditText。
  • ExtractEditText:它并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">
    <TableRow android:paddingStart="20dp"
        android:paddingEnd="20dp" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="16sp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请填写登录账号"
            android:selectAllOnFocus="true"/>
    </TableRow>
    <TableRow android:paddingStart="20dp"
        android:paddingEnd="20dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="16sp"/>
        <!-- android:inputType="numberPassword"表明只能接受数字密码 -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberPassword"/>
    </TableRow>
    <TableRow android:paddingStart="20dp"
        android:paddingEnd="20dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="年龄:"
            android:textSize="16sp"/>
        <!-- inputType="number"表明是数值输入框 -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"/>
    </TableRow>
    <TableRow android:paddingStart="20dp"
        android:paddingEnd="20dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="生日:"
            android:textSize="16sp"/>
        <!-- inputType="date"表明是日期输入框 -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="date"/>
    </TableRow>
    <TableRow android:paddingStart="20dp"
        android:paddingEnd="20dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="电话号码:"
            android:textSize="16sp"/>
        <!-- inputType="phone"表明是输入电话号码的输入框 -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请填写您的电话号码"
            android:selectAllOnFocus="true"
            android:inputType="phone"/>
    </TableRow>
    <Button
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"/>
</TableLayout>

直接运行

Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法文章来源地址https://www.toymoban.com/news/detail-457801.html

到了这里,关于Android学习笔记 2.3.1 文本框TextView和编辑框EditText的功能和用法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android 之 TextView (文本框)详解

    学习完Android中的六大布局,从本节开始我们来一个个讲解Android中的UI控件,本节给大家带来的UI控件是:TextView(文本框),用于显示文本的一个控件,另外声明一点,我不是翻译API文档,不会一个个属性的去扣,只学实际开发中常用的,有用的,大家遇到感觉到陌生的属性可以

    2024年02月09日
    浏览(30)
  • Android根据TextView的文本大小计算其宽度和高度

    Android根据TextView的文本大小计算其宽度和高度 在Android开发中,TextView是常用的UI组件之一,用于显示文本内容。有时候我们需要根据TextView的文本大小来动态计算其宽度和高度,以便适应不同长度的文本内容。本文将介绍如何使用Java代码来实现这一功能。 首先,我们需要获取

    2024年02月02日
    浏览(33)
  • Android TextView 加载MarkDown 格式的数据,支持富文本格式

    前言:项目开发中经常需要一些文字显示不同样式 比如:加粗 换行 添加图片 链接调整等等,这个时候大都用安卓自带的Spannable, 如果特殊样式的字体比较少Spannable或者html 还是可以的。但是如果客户端要支持复杂样式的文案 Spannable 就不是非常灵活了 ,这种情况下很多小伙

    2024年02月05日
    浏览(34)
  • Android TextView 加入滚动功能

    安卓的TextView,一页放不下,就需要用到滚动功能,代码如下,加入ScrollView即可。 参考: Android设置TextView可滚动_互联网小熊猫的博客-CSDN博客_android textview 滚动 总结:这个方法虽然可以使用,但是滚动的效果很不好,PASS. 参考:Android TextView更新内容后自动滑行到最后一行

    2024年02月13日
    浏览(49)
  • WPF实战学习笔记17-TodoView 添加新增、编辑、查询功能

    修改TodoViewModel.cs 修改XAML 添加引用 添加绑定 添加项目的双击事件 修改ToDoService 修改MyToDo.Api/Service/ToDoService.cs

    2024年02月15日
    浏览(30)
  • Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能

    效果图  安装codemirror依赖 本示例为Vue2项目,安装低版本的依赖 实现 实现代码如下,里边涉及到的变量和函数自行替换即可,没有其他复杂逻辑。

    2024年02月10日
    浏览(28)
  • android 富文本编辑器有哪些

    android 富文本编辑器有哪些 有许多优秀的开源富文本编辑器插件可用于Android平台,下面列举几个常用的: RichEditorView:这是一个基于Web技术的富文本编辑器插件,有多种编辑功能与选项。 Android Rich Text Editor:这是一个轻量级的富文本编辑器插件,支持加粗、斜体、下划线等

    2024年02月15日
    浏览(37)
  • 若依框架图片上传、富文本框编辑器功能

    现在的需求是:实现一个项目展示模块,后端管理页面除了需要基础信息外,要加上一个 图片上传和富文本框编辑器功能 。 点击”图片存储地址”:可上传电脑任何位置的图片,并可对图片进行放大,缩小,和旋转。 存入数据库的图片以url地址存放 url直接百度可看到图片

    2024年04月13日
    浏览(46)
  • Vim是一款功能强大的文本编辑器

    简介: Vim是一款功能强大的文本编辑器,广泛用于Linux系统。以下是Vim编辑器的基本使用方法: 打开文件: 使用vim命令加上要编辑的文件名,例如:vim filename。 进入编辑模式: 在Vim中,有多种模式,初始状态是命令模式,需要按下i键或a键进入编辑模式。 编辑文本: 在编

    2024年04月23日
    浏览(34)
  • 界面控件DevExpress WPF富文本编辑器,让系统拥有Word功能(二)

    DevExpress WPF控件的富文本编辑器允许开发者将文字处理功能集成到下一个WPF项目中,凭借其全面的文本格式选项、邮件合并以及丰富的终端用户选项集合,可以轻松地提供Microsoft Word功能。 DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用

    2024年02月06日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包