zoukankan      html  css  js  c++  java
  • Android 重写EditText回车事件

    之前遇到的问题没来得及记录下来,趁今晚有空就重新回忆并写下了。

    我们在用到EditText这个空间时经常需要重写软键盘中的回车事件以配合我们接下来的响应,比如点击回车变成搜索、发送、完成等。

    EditText为我们提供了一个属性imeOptions用来替换软键盘中enter键的外观,如actionDone会使外观变成“完成”。

    下面列出比较经常用到的几个属性以及替换的文本外观:

      actionUnspecified        未指定         EditorInfo.IME_ACTION_UNSPECIFIED.  
      actionNone                 动作            EditorInfo.IME_ACTION_NONE 
      actionGo                    去往            EditorInfo.IME_ACTION_GO
      actionSearch               搜索            EditorInfo.IME_ACTION_SEARCH    
      actionSend                 发送            EditorInfo.IME_ACTION_SEND   
      actionNext                下一项           EditorInfo.IME_ACTION_NEXT   
      actionDone               完成              EditorInfo.IME_ACTION_DONE 

    设置的方法可以在布局文件中设置 android:imeOptions="actionNext" 或者在代码中 mUserEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);

    接下来就需要重写回车事件了,通过setOnEditorActionListener

    private void initListener() {
            mUserEdit
                    .setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        public boolean onEditorAction(TextView v, int actionId,
                                KeyEvent event) {if (actionId == EditorInfo.IME_ACTION_SEND
                                    || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    //让mPasswordEdit获取输入焦点 mPasswordEdit.requestFocus();
    return true; } return false; } }); }

    到此重写回车事件就完成了。

    下面顺便列出几个edittext常用的属性:

    android:password="true"  这条可以让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。

    android:numeric="true" 这条可以让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入

    android:capitalize="abcde" 这样仅允许接受输入abcde,一般用于密码验证

    android:hint="密码"  设置显示的提示信息

    android:singleLine="true"  设置单行输入,这样就不会自动换行

  • 相关阅读:
    网站搜索功能lucene
    RabbitMQ消息队列
    zookeeper
    RPC+SOA+dubbo
    石英定时任务-quartz
    通用mapper、图片上传、nginx
    通用mapper和分类实现
    后台商品管理功能实现
    构建框架
    海量数据的并发处理
  • 原文地址:https://www.cnblogs.com/kkrimen/p/3414849.html
Copyright © 2011-2022 走看看