介绍

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() {@Overridepublic 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

布局定义

    <AutoCompleteTextViewandroid: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" />

代码实现

 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Get a reference to the AutoCompleteTextView in the layoutAutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);// Get the string arrayString[] countries = getResources().getStringArray(R.array.countries_array);// Create the adapter and set it to the AutoCompleteTextViewArrayAdapter<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

转载于:https://www.cnblogs.com/lanzhi/p/6467166.html

Android 基础(二十四) EditText相关推荐

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

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

  2. Android笔记(二十四):gradle写一个android12自动适配exported脚本,支持aab

    背景 由于Google play的政策,提审aab的时候需要适配android12,适配android12最大的工作就是在AndroidManifesst.xml文件中声明的四大组件,都要显式声明ex ...

  3. 好记性不如烂笔杆-android学习笔记十四 EditText 画行,解决光标压线问题

    这个问题我在网上看来很多方法,有些看得似懂非懂的,后来自己采用一种比较投机的方法,居然可行,呵呵,拿出来跟大家分享一下. 其实就是把画的线向下偏移一定高度,当然针对不同分辨率,这个值也就不同: (因为 ...

  4. 零基础带你学习MySQL—not null 非空(二十四)

    零基础带你学习MySQL-not null 非空(二十四)

  5. mysql循环查询一个表中的数据并进行修改_JavaScript学习笔记(二十四)-- MYSQL基础操作...

    MYSQL mysql 是一个数据库的名字 和 php 合作的比较好的数据库 之前我们说过一个问题,前端向后端索要数据,后端就是去数据库中查询数据,返回给前端 接下来就聊聊使用 php 操作数据库 M ...

  6. JAVASE基础模块二十四(泛型)

    JAVASE基础模块二十四(泛型) 泛型的引出 我们这样定义一个obj类的时候 使用的时候需要向下转型 会很麻烦 public class Fananan {public static void ma ...

  7. [系统安全] 二十四.逆向分析之OllyDbg调试INT3断点、反调试、硬件断点与内存断点

    您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...

  8. SAP UI5 初学者教程之二十四 - 如何使用 OData 数据模型试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 初学者教程之一:Hello World SAP UI5 初学者教程之二:SAP UI5 ...

  9. (100)详细描述一个你做过的项目, 面试必问(二十四)(第20天)

    (100)详细描述一个你做过的项目, 面试必问(二十四)(第20天) 1 文章目录 1)文章目录 2)FPGA初级课程介绍 3)FPGA初级课程架构 4)详细描述一个你做过的项目, 面试必问(二十四) ...

  10. go设置后端启动_Go语言基础(十四)

    Go语言基础(十四) 一.Redis 二.NSQ 三.Go module 四.Context......0 一.Redis Redis是一个key-value存储系统.和Memcached类似,它支持 ...

最新文章

  1. Silverlight RIA Services基础专题
  2. 线性Transformer应该不是你要等的那个模型
  3. 丰厚奖学金博士招生 | 澳大利亚OPTIMA 招募博士,多光谱时间序列数据的时空目标检测/分割方向...
  4. Facebook 开源 3D 深度学习函数库 PyTorch3D,也可用于二维场景
  5. 全新的 Discuz! Q 来了!
  6. android判断是否被点击方法,android 中有没有判断imageview是否以被单击的函数方法?...
  7. python语言的读法-Python语言的特点及自学建议
  8. 无线路由不能上网问题的解决的方法
  9. 防护针对SQL Server数据库的SQL注入攻击
  10. C# List用法;已经实例测试OK,直接可用!List 增加减少元素,输出元素
  11. 信号课组(一) 信号与系统 Review 1 信号与系统综述
  12. 谈谈如何做到从未来看向当代的能源技术
  13. C++实现动态规划算法之解决0-1背包问题
  14. c语言名人名言大全摘抄,语文摘抄名人名言
  15. 17AHU排位赛3 D题 旋转吧!雪月花 ! (DFS序,线段树维护树上最值)
  16. ERR_NAME_NOT_RESOLVED错误的解决方法
  17. 关于惠普系列电脑无法开机以及其他问题解决的总结
  18. vue3—reactive如何更改属性
  19. html5学生成绩表,学生成绩表excel
  20. html5 人在星空中,《在星空中》的教学设计

热门文章

  1. java 对话框 位置_JAVAJOptionPane对话框的几种方法
  2. 判断有向图g中顶点i到顶点j是否有路径_号称图的最短路径算法--Floyd算法
  3. mysqlclient==1.3.7对应mysql版本_Python通过MySQLdb访问操作MySQL数据库
  4. Lex-BERT:超越FLAT的中文NER模型?
  5. java第七章多线程_第七章 多线程
  6. element ui 批量删除之后动态更新列表_气象编程 | Python高效批量绘图方法
  7. 【解决】Authentication plugin 'caching_sha2_password' cannot be loaded
  8. 【企业管理】组织与管理的思考
  9. 【科普】一图区分 IAAS + PAAS + SAAS
  10. 转-项目管理心得:一个项目经理的个人体会、经验总结