安卓基础之《(9)—中级控件(3)文本输入》

一、编辑框EditText

1、编辑框EditText用于接收软键盘输入的文字,例如用户名、密码、评价内容等,它由文本视图派生而来,除了TextView已有的各种属性和方法,EditText还支持下列XML属性

(1)inputType:指定输入的文本类型。若同时使用多种文本类型,则可使用竖线"|"把多种文本类型拼接起来

|----------------|-------------------------------|
| 输入类型 | 说明 |
| text | 文本 |
| textPassword | 文本密码。显示时用圆点"·"代替 |
| number | 整型数字 |
| numberSigned | 带符号的数字。允许在开头带负号"-" |
| numberDecimal | 带小数点的数字 |
| numberPassword | 数字密码。显示时用圆点"·"代替 |
| datetime | 时间日期格式。除了数字外,还允许输入横线、斜杆、空格、冒号 |
| date | 日期格式。除了数字外,还允许输入横线"-"和斜杆"/" |
| time | 时间格式。除了数字外,还允许输入冒号":" |

(2)maxLength:指定文本允许输入的最大长度

(3)hint:指定提示文本的内容

(4)textColorHint:指定提示文本的颜色

2、例子

activity_edit_simple.xml

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:orientation="vertical"
    android:padding="5dp"
    tools:context=".EditSimpleActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="start"
        android:text="下面是登录信息"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:inputType="text"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword"/>


</LinearLayout>

3、设置边框

可以去除边框,或者给边框设置颜色

activity_edit_border.xml

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:orientation="vertical"
    tools:context=".EditBorderActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是默认的边框"
        android:inputType="text"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="我的边框不见了"
        android:inputType="text"
        android:background="@null"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="我的边框是圆角"
        android:inputType="text"
        android:background="@drawable/edit_text_selector"/>

</LinearLayout>

edit_text_selector.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus" />
    <item android:state_focused="false" android:drawable="@drawable/shape_edit_normal" />
</selector>

shape_edit_focus.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 指定了形状内部的填充颜色 -->
    <solid android:color="#ffffff" />

    <!-- 指定了形状轮廓的粗细与颜色 -->
    <stroke android:width="1dp" android:color="#0000ff" />

    <!-- 指定了形状四个圆角的半径 -->
    <corners android:radius="5dp" />

    <!-- 指定了形状四个方向的间距 -->
    <padding android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>

shape_edit_normal.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 指定了形状内部的填充颜色 -->
    <solid android:color="#ffffff" />

    <!-- 指定了形状轮廓的粗细与颜色 -->
    <stroke android:width="1dp" android:color="#aaaaaa" />

    <!-- 指定了形状四个圆角的半径 -->
    <corners android:radius="5dp" />

    <!-- 指定了形状四个方向的间距 -->
    <padding android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
    
</shape>
相关推荐
消失的旧时光-19436 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
Jinkxs6 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&6 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
LDORntKQH7 小时前
基于深度强化学习的混合动力汽车能量管理策略 1.利用DQN算法控制电池和发动机发电机组的功率分配 2
android
冬奇Lab7 小时前
Android 15 ServiceManager与Binder服务注册深度解析
android·源码·源码阅读
2501_916008899 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
独行soc10 小时前
2026年渗透测试面试题总结-20(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
常利兵10 小时前
2026年,Android开发已死?不,它正迎来黄金时代!
android
Risehuxyc10 小时前
备份三个PHP程序
android·开发语言·php
Doro再努力20 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim