介绍

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion.

You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a < EditText > element.

文本区域给用户输入文字提供了方便,它可以是单行的也可以是多行的。触摸一个文本区域获取光标并且自动弹出键盘。除了可以输入文字,文本区域还可以用来执行如文本选中(剪切,复制和粘贴)和通过自动补全实现的数据查询等功能。你可以通过在布局中添加一个EditText对象来实现文本区域,当然也可以在布局文件中添加EditText Tag.

指定键盘类型

Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.

You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. For example, if you want the user to input an email address, you should use the textEmailAddress input type:

文本区域有不同的输入类型,如数字,日期,密码或者邮箱地址。输入类型决定了文本区域内允许输入的字符类型,同时也提示着虚拟键盘给用户展示更加常用的字符集合。我们可以通过使用 android:inputType 属性来明确我们的输入框想要的键盘类型。例如,如果你想要输入邮箱地址,你可以使用textEmailAddress输入类型。

<EditText
android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
android:inputType="textEmailAddress" />
android:inputType Notice
“none” 不弹出键盘
“text” 普通文本键盘
“textEmailAddress” 普通文本键盘,”@”
“textUri” 普通文本键盘,”/”
“number” 数字键盘
“phone” Phone-Style键盘

控制其他行为

android:inputType属性不仅可以用来控制显示特定的键盘类型,而且还可以用来明确一些键盘的行为,例如是否大写,自动补全或者是拼写建议等。android:inputType使用按位与的方式,所以可以指定多种行为

android:inputType Notice
“textCapSentences” 正常的文本键盘但是每一句话第一个字母大写
“textCapWords” 正常文本键盘但是每一个单词的第一个字母大写
“textAutoCorrect” 自动提示拼写错误
“textPassword” 输入的文字都变成点点
“textMultiLine” 允许输入多行文本,可以换行

关于android:inputType属性有很多,上面都只是一些栗子,感觉应该很全了,大家可以自行研究。


指定键盘行为

除了改变键盘的输入类型,Android允许当用户完成输入指定一个动作,出现的操作指定按钮的回车键和动作,如“搜索”或“发送”键。

例如:

<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:inputType="text"
android:imeOptions="actionSearch" />

键盘会显示出“搜索”按钮,横屏之后不仅键盘会出现搜索按钮,右侧还会出现对应的字串





主要是对 android:imeOptions 属性的使用

这么多的行为可供选择,上面只是举一个栗子


对键盘中定义的行为的响应

通过android:imeOptions 定义的行为,我们可以对它定义的行为作出响应.我们可以使用TextView.OnEditorActionListener来进行事件的监听和处理。通过如 IME_ACTION_SEND 或者IME_ACTION_SEARCH等事件ID来针对不同的事件行为做出不同的处理。

例如

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});

自定义输入完成后键盘行为的标签

android:imeActionLabel属性就是用来设置这个内容的

<EditText
android:id="@+id/launch_codes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_launch_codes"
android:inputType="number"
android:imeActionLabel="启动" />


自动补全建议

使用AutoCompleteTextView

布局定义

    <AutoCompleteTextView
android:completionThreshold="1"//默认是2,这就是为什么默认输入两个字符才能显示出提示信息的原因
android:id="@+id/autocomplete_country"
android:layout_width="match_parent"
android:text="@android:string/dialog_alert_title"
android:layout_height="wrap_content" />

代码实现

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);//设置提示内容
}

提示数组

    <string-array name="countries_array">
<item>Afghanistan</item>
<item>Albania</item>
<item>Algeria</item>
<item>American Samoa</item>
<item>Andorra</item>
<item>Angola</item>
<item>Anguilla</item>
<item>Antarctica</item>
</string-array>


备注

内容主要来自于Android官网教程:

https://developer.android.com/guide/topics/ui/controls/text.html

<Android 基础(二十四)> EditText的更多相关文章

  1. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  2. Android笔记二十四.Android基于回调的事件处理机制

        假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...

  3. Android进阶(二十四)Android UI---界面开发推荐颜色

    Android UI---界面开发推荐颜色   在Android开发过程中,总要给app添加一些背景,个人认为使用纯色调便可以达到优雅的视觉效果. 补充一些常用的颜色值:colors.xml < ...

  4. <Android 基础(十四)> selector

    介绍 A StateListDrawable is a drawable object defined in XML that uses a several different images to r ...

  5. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  6. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

  7. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

  8. Bootstrap <基础二十五>警告(Alerts)

    警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...

  9. Bootstrap <基础二十二>超大屏幕(Jumbotron)

    Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...

  10. Bootstrap<基础二十> 标签

    Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...

随机推荐

  1. C#-WebForm-AJAX阿贾克斯(二)★★★★★ajax的完整结构★★★★★

    ajax完整结构: $.ajax({ url:"",//服务器路径 data:{},//给服务端传递的参数,可以没有,也可以是多个 type:"post", / ...

  2. Java NIO学习与记录(六): NIO线程模型

    NIO线程模型 上一篇说的是基于操作系统的IO处理模型,那么这一篇来介绍下服务器端基于IO模型和自身线程的处理方式. 一.传统阻塞IO模型下的线程处理模式 这种处理模型是基于阻塞IO进行的,上一篇讲过 ...

  3. LOJ2229. 「BJOI2014」想法(随机化)

    题目链接 https://loj.ac/problem/2229 题解 评分标准提示我们可以使用随机化算法. 首先,我们为每一道编号在 \([1, m]\) 以内的题目(这些题目也对应了 \(m\) ...

  4. Exponentiation POJ-1001

    http://poj.org/problem?id=1001 //10000000 100000 #include<iostream> #include<cstring> us ...

  5. (转)史上最全的MSSQL复习笔记

    1.什么是SQL语句 sql语言:结构化的查询语言.(Structured Query Language),是关系数据库管理系统的标准语言. 它是一种解释语言:写一句执行一句,不需要整体编译执行.语法 ...

  6. 【Kafka源码】Kafka启动过程

    一般来说,我们是通过命令来启动kafka,但是命令的本质还是调用代码中的main方法,所以,我们重点看下启动类Kafka.源码下下来之后,我们也可以通过直接运行Kafka.scala中的main方法( ...

  7. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. PHP之string之ltrim()函数使用

    ltrim (PHP 4, PHP 5, PHP 7) ltrim - Strip whitespace (or other characters) from the beginning of a s ...

  9. 错误:‘lock_guard’ 在此作用域中尚未声明

    解决:修改报错文件,加入#include <boost/thread/lock_guard.hpp>

  10. ASP.NET Core 集成 WebSocket

    1. 环境 AspNetCore Web 2.0 (MVC) Windows 10 IIS 10 Express/IIS VS 2017 2.如何配置 在已有的或者新创建的 AspNet Core M ...