键盘事件 OnEditorAction

好久之前解决的一个问题, 点击返回键 或者 点击键盘的 done 当时会发生控制再也无法操作的问题

在EditText中,可以使用setImeOptions()方法来来开启软键盘的”Done”按钮。
示例代码如下:editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

按下”Done”按钮的默认行为是关闭软键盘,但是我们可以通过EditText的setOnEditorActionListener()方法

 EditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {  

    @Override  
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  

        // EditorInfo.IME_ACTION_GO          android:imeOptions="actionGo"               
        // EditorInfo.IME_ACTION_SEARCH      android:imeOptions="actionSearch" 
        // EditorInfo.IME_ACTION_SEND        android:imeOptions="actionSend"  
    // EditorInfo.IME_ACTION_NEXT        android:imeOptions="actionNext"
    // EditorInfo.IME_ACTION_UNSPECIFIED  android:imeOptions="actionUnspecified"  

    if (actionId == EditorInfo.IME_ACTION_DONE) {  android:imeOptions="actionDone" 

            return true;    
        }  
        return false;  
    }  

EditorInfo.IME_ACTION_DONE可以和其他的标志一起组合使用来设置软键盘,比如:
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE);

在输入法弹出的情况下 仍然接收处理 back



public class EditText1 extends EditText {

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

        }
        return super.dispatchKeyEventPreIme(event);

 }

或者在 onKeyPreIme 方法

   @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK ){
            this.clearFocus();
            InputMethodManager inputMethodManager =(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(this.getWindowToken(), 0);
        }
        return super.onKeyPreIme(keyCode, event);
    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值