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

智能推荐

wenet编译报Do not know how to define a 32-bit integer quantity on your system-程序员宅基地

文章浏览阅读1.5k次。wenet编译报错_do not know how to define a 32-bit integer quantity on your system! neither

windows11下配置visual studio 2022 nginx工程_nginx visual studio-程序员宅基地

文章浏览阅读699次,点赞2次,收藏3次。windows11下配置visual studio 2022 nginx工程_nginx visual studio

emacs配置教程_emacs设置-程序员宅基地

文章浏览阅读483次。自定义emacs配置,可以进行一步到位的配置工作_emacs设置

python常见问题解析:浅析python 动态库m.so.1.0错误问题_libpython3.9m.so.1.0-程序员宅基地

文章浏览阅读3.2k次。@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府这篇文章主要介绍了python 动态库m.so.1.0错误问题,文中给大家提到了python中使用动态库的方法,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下$ python -V python: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No _libpython3.9m.so.1.0

数据挖掘十大算法--Apriori算法-程序员宅基地

文章浏览阅读4k次,点赞19次,收藏23次。Apriori 算法是一种用于关联规则挖掘的经典算法。它用于在大规模数据集中发现频繁项集,进而生成关联规则。关联规则揭示了数据集中项之间的关联关系,常被用于市场篮分析、推荐系统等应用。Apriori 算法的主要优点是它相对简单,并且易于理解和实现。然而,在处理大规模数据集时,它可能面临性能挑战。后续的改进算法,如 FP-Growth 等,通过不同的方式优化了频繁项集的发现过程,提高了算法的效率。_apriori算法

零基础教程:R语言lavaan结构方程模型(SEM)-程序员宅基地

文章浏览阅读1.2k次,点赞17次,收藏12次。基于R语言lavaan程序包,通过理论讲解和实际操作相结合的方式,由浅入深地系统介绍结构方程模型的建立、拟合、评估、筛选和结果展示的全过程。我们筛选大量经典案例

随便推点

windows任务计划程序运行python脚本失败的原因_任务计划程序无法启动python.exe-程序员宅基地

文章浏览阅读5.6k次,点赞14次,收藏9次。windows任务计划程序运行python脚本直接闪退?运行结果显示(0x1)?引发了FileNotFoundError异常?_任务计划程序无法启动python.exe

jenkins自动拉取git代码并运行_windows jenkins调用git bash-程序员宅基地

文章浏览阅读1.2k次。当我们的Jenkins要在某一台固定的电脑执行,我们则要将这台电脑加到Jenkins里。操作步骤如下:1、节点管理点击Jenkins首页,然后点击系统管理–节点管理,进入到节点管理页面2、点击新建节点3、在执行(测试)机器上,我们可以同时执行几个任务。远程工作目录:即我们放置代码的目录,我们要执行的代码的目录![在这里插入图片描述](https://img-blog.csdnimg.cn/20200618192134332.pngUsage:(1)尽可能的使用这个节点:即既可以_windows jenkins调用git bash

Freesurfer tutorial主要内容_brodmann and exvio ec labels-程序员宅基地

文章浏览阅读4.1k次,点赞2次,收藏17次。Introduction to Freesurfer Outputhttps://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/OutputData_freeviewhttps://blog.csdn.net/qq_28480795/article/details/105094883本教程旨在介绍Freeview。有关此处未介绍的按钮或菜单选项的信息,您可以在FreeviewGuide wiki上找到。主要内容:如何可视化和检查FreeSurfer数据_brodmann and exvio ec labels

java基于微信小程序的奶茶点餐系统 奶茶店奶茶点餐小程序(源码(1)-程序员宅基地

文章浏览阅读263次,点赞3次,收藏4次。近年来,随着微信的快速发展,基于微信平台的应用层出不穷,许多商家也将点餐系统部署到了微信平台上,顾客可以从微信直接登录到点餐系统中进行菜品浏览,下单与支付,这使得点餐变得更加方便与快捷。本课题针对奶茶店设计出一个奶茶点餐微信小程序,奶茶点餐小程序可以帮助传统实体奶茶店在线上实现奶茶的售卖,并且使顾客也能更加方便快捷地购买到自己想要的奶茶饮品。博主介绍:全网粉丝10W+,CSDN全栈领域优质创作者,博客之星、掘金/华为云/阿里云等平台优质作者。 精彩专栏 推荐订阅。

Keil for ARM-MDK的使用_keil for arm mdk-程序员宅基地

文章浏览阅读4.3k次。概念:μVision:是Keil的文本编辑界面。目前的μVision支持代码自动补全。如果想使用外部的文本编辑器,可以在Tools-Customize Tools Menu设置。MDK:就是Keil for ARM。全称Microcontroller Development Kit。具体可以参考这里的说明。调试方法:调试方法分两种:软件仿真和硬件调试,即Simulator和Debugger。_keil for arm mdk

mpeg4 码流格式及判断关键帧_mpeg4 关键帧-程序员宅基地

文章浏览阅读3.3k次。mpeg4 码流格式及判断关键帧_mpeg4 关键帧

推荐文章

热门文章

相关标签