Android重写EditText回车等事件

重写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”
在代码中 edittext.setImeOptions(EditorInfo.IME_ACTION_NEXT);

    edittext.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
    edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            //此处会响应2次 分别为ACTION_DOWN和ACTION_UP
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&
             event.getAction() == KeyEvent.ACTION_DOWN) {

                return true;
            }
            return false;
        }
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值