android软键盘的事件响应:android EditText inputType 及 android:imeOptions=”actionDone”_android actiondone-程序员宅基地

技术标签: EditText  android  android软键盘  Android软键盘弹出  

一、android 软件盘事件响应
在android中,有时需要对EditText实现软件盘监听的场景。当android按下软键盘的时候,响应完成、发送、搜索或者其他事件。
Google 提供了 EditorInfo、 KeyEvent 的一些方法,能够实现我们需要的功能。详细可研究:EditorInfo.class 和 KeyEvent.class.
 
输入回车键隐藏输入键盘的方法:

如果布局中包含多个EditText,可以为每个EditText控件设置android:singleLine=”true”,弹出的软盘输入法中回车键为next,直到最后一个获取焦点后显示为Done。点击Done后,隐藏软键输入盘。将EditText的imeOptions属性设置android:imeOptions=”actionDone”,则不管是不是最后一个EditText,点击回车键即隐藏输入法。

监听Enter的事件,编写Enter的事件响应。设置文本框的OnKeyListener,当keyCode ==KeyEvent.KEYCODE_ENTER的时候,表明Enter键被按下,就可以编写自己事件响应功能了。

XML文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<EditText
          android:id= "@+id/editTextId"
          android:layout_width= "fill_parent"
          android:layout_height= "50dp"
          android:imeOptions= "actionDone"
          android:hint= "@string/task_new_one"
          android:textSize= "15sp"
          android:singleLine= "true"
          android:paddingLeft= "5dp"
          android:layout_gravity= "center"
          android:background= "@drawable/rectangle"
          android:inputType= "text"
          >
  </EditText>

  

 

把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案。
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 一个放大镜
actionSend : Send
actionNext : Next
actionDone : Done,隐藏软键盘,即使不是最后一个文本输入框

通过修改 android:imeOptions 来改变默认的键盘显示文本。常用的常量值如下:
  1. actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果: 
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:
     
JAVA代码:
1
2
EditText inputText = (EditText) findViewById(R.id. editTextId);  
inputText.setImeOptions(EditorInfo.IME_ACTION_DONE);

  

添加监听事件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private final EditText.OnEditorActionListener editorActionListener =
            new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_DONE) {
                        //业务代码
                        haoMent.createTest(Test.getId(), v.getText().toString());
                        UiUtils.hideSoftKeyboard(getApplicationContext(), haoTest. this );
                        v.setText( "" );
                        v.clearFocus();
                        handler.post(updateView);
                    }
                    return true ;
                }
           };

  

            
但是,如果手机的输入法不是内置输入法,而是其他第三方输入法,那么可能会发生软件盘回车键无响应的问题。为了防止该类事情,则增加红色部分,响应的其KeyEvent。
这时候需要在代码中添加事件响应。

inputKey = (EditText) findViewById(R.id.contactSearch_editText);
inputKey.addTextChangedListener(watcher);

inputKey.setOnKeyListener(new View.OnKeyListener() {
@Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    

  if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) {
    handler.post(updateView);
    return true;
  }
  return false;
  }
});
//响应键盘内容
public TextWatcher watcher = new TextWatcher() {

  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i2,int i3) {

  }

  @Override
  public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

  }

  @Override
  public void afterTextChanged(Editable editable) {

  handler.post(updateView);

  }
};

 
 
二、android 输入类型
根据要输入的内容展现相应的软件盘,可通过修改 android:inputType 来实现。
这是一些常用的输入类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
android:inputType= "none" --输入普通字符
android:inputType= "text" --输入普通字符
android:inputType= "textCapCharacters" --输入普通字符
android:inputType= "textCapWords" --单词首字母大小
android:inputType= "textCapSentences" --仅第一个字母大小
android:inputType= "textAutoCorrect" --前两个自动完成
android:inputType= "textAutoComplete" --前两个自动完成
android:inputType= "textMultiLine" --多行输入
android:inputType= "textImeMultiLine" --输入法多行(不一定支持)
android:inputType= "textNoSuggestions" --不提示
android:inputType= "textUri" --URI格式
android:inputType= "textEmailAddress" --电子邮件地址格式
android:inputType= "textEmailSubject" --邮件主题格式
android:inputType= "textShortMessage" --短消息格式
android:inputType= "textLongMessage" --长消息格式
android:inputType= "textPersonName" --人名格式
android:inputType= "textPostalAddress" --邮政格式
android:inputType= "textPassword" --密码格式
android:inputType= "textVisiblePassword" --密码可见格式
android:inputType= "textWebEditText" --作为网页表单的文本格式
android:inputType= "textFilter" --文本筛选格式
android:inputType= "textPhonetic" --拼音输入格式
android:inputType= "number" --数字格式
android:inputType= "numberSigned" --有符号数字格式
android:inputType= "numberDecimal" --可以带小数点的浮点格式
android:inputType= "phone" --拨号键盘
android:inputType= "datetime"
android:inputType= "date" --日期键盘
android:inputType= "time" --时间键盘

 

密码框属性 android:password="true"   让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。
纯数字 android:numeric="true"      让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入
仅允许 android:capitalize="haoTest"   仅允许接受输入haoTest,一般用于密码验证
android:editable="false"         设置EditText不可编辑
android:singleLine="true"        强制输入的内容在单行
android:ellipsize="end"         自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时
(部分属性参考网络:http://blog.csdn.net/lushengchu_luis/article/details/8699791、http://www.eoeandroid.com/thread-313140-1-1.html)
 
 
转载请注明出处:http://www.cnblogs.com/haochuang/
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_29586601/article/details/78753480

智能推荐

获取Form.ShowDialog() 出的窗体中textbox的返回值_this.getformref().-程序员宅基地

文章浏览阅读540次。转自 : http://blog.csdn.net/piaofengxiyue/article/details/4494382经常会遇到ShowDialog出的窗体中有一些textbox,输入完后返回到主窗体中。怎么办呢?有办法。 理论上讲ShowDialog后的窗体返回值类型是DialogResult。也就是一个枚举值,只有yes,no,ok之类的值,肯定是不_this.getformref().

Android基础——类加载器和动态加载_动态加载aar-程序员宅基地

文章浏览阅读1.7k次。关于动态加载和类加载器的简单理解_动态加载aar

HTML5——history实现单页面_单个html文件怎么实现单页面应用-程序员宅基地

文章浏览阅读2.5k次。1. 单页Web应用(single page web application,SPA)history.back():URL回退一次history.forward():URL前进一次history.go(n):URL几次history.back()或几次history.forward(),比如:history.go(2):两次history.forward();history.go(-2..._单个html文件怎么实现单页面应用

XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax...-程序员宅基地

文章浏览阅读195次。本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件XML来源SGMLSGML(SGM)标准通用标记语言是一种定义电子文档结构和描述其内容的国际标准语言,具有极好的扩展性是所有电子文档标记语言的起源,早在万维网发明之前“通用标言”就已存在是1..._public int toolremindertotal()的xml如何写

工控机主机该怎么加固_工控机用固定-程序员宅基地

文章浏览阅读2.9k次。工控安全现状工业控制系统是支撑国民经济的重要设施,是工业领域的神经中枢。现在工业控制系统已经广泛应用于电力、通信、化工、交通、航天等工业领域,支撑起国计民生的关键基础设施。随着传统的工业转型,数字化、网络化和智能化的工业控制系统逐渐接入互联网,病毒、木马、蠕虫、僵尸网络等常见威胁也威胁到工业控制系统的安全。近几年,勒索病毒的出现,在企业损失大量数据的情形下,也对企业造成了不可估量的经济损失。目前,企业会在工业控制系统的外围建立防火墙、入侵检测系统、入侵防御系统等技术控制手段,同时也会采用不必.._工控机用固定

小米商城项目总结_小米商城项目总结心得-程序员宅基地

文章浏览阅读7.2k次。脚本中判断是否相等不能用equals,没有这个方法,只能用== 数据库工具类,只需要加载一次就够了,所以我们采用静态代码块来初始化该方法,作为工具类使用 public static QueryRunner qr = null;//因为是在不同包下的,所以要用public才可以调用 Ajax往Servlet发送数据的时候Servlet必须response一个数据,不然会报parsee..._小米商城项目总结心得

随便推点

HDFS的EditLog和FsImage作用详细解析,超详细!(含部分非原创图片,大部分原创总结)_hdfs editlog-程序员宅基地

文章浏览阅读698次,点赞7次,收藏10次。EditLog和FsImage的概念,以及与SecondaryNameNode的关系问题,以及EditLog和FsImage的重要性问题。_hdfs editlog

JS 系列之 事件—表单事件_js input event-程序员宅基地

文章浏览阅读2.6k次。今天跟大家分享一下JS系列之表单事件。1 表单事件的种类1.1 input 事件input事件当、、的值发生变化时触发。对于复选框()或单选框(),用户改变选项时,也会触发这个事件。另外,对于打开contenteditable属性的元素,只要值发生变化,也会触发input事件。input事件的一个特点,就是会连续触发,比如用户每按下一次按键,就会触发一次input事件。input事件对象..._js input event

菜单下拉条-程序员宅基地

文章浏览阅读82次。最近 ,用到了一些js的知识,稍微复习了下,还把之前写的js代码又复习了一遍,这里贴上来!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http:/..._菜单下拉条

Windows Terminal完整指南-程序员宅基地

文章浏览阅读2.4k次,点赞4次,收藏29次。在本文中,我们将探讨Windows Terminal,它是WSL2的理想配套。它速度快、可配置、外观漂亮,并且提供了Windows和Linux开发的所有优点。Windows已经完全接受了Linux,而WSL2使它成为一种无缝的乐趣。你可以通过以下方式访问发行版的终端:单击其开始菜单图标在Powershell或命令提示符下输入 wsl 或 bash通过启动 %windir%\system32\bash.exe ~ 的配置文件使用第三方终端选项(例如Cmder,ConEmu和Hyper)在VS .

To install them, you can run: npm install --save core-js/modules/es.array.push.js-程序员宅基地

文章浏览阅读5k次,点赞5次,收藏8次。To install them, you can run: npm install --save core-js/modules/es.array.push.js core-js/modules/es.error.cause.js core-js/modules/es.object.proto.js core-js/modules/es.re

wpf 界面切换-程序员宅基地

文章浏览阅读1.3w次,点赞3次,收藏30次。界面切换和窗口跳转是所有桌面程序都需要的。 wpf里面也有很多方法来做到界面切换,简单常用的有TabControl和UserControl。TabControl优点是简单,继续拖控件。 缺点就是全部代码会堆到一起。代码&lt;TabControl HorizontalAlignment="Left" Height="200" Margin="10,10,0,0" Vert...

推荐文章

热门文章

相关标签