为了账号安全,请及时绑定邮箱和手机立即绑定

如何处理ImeOptions的完成按钮单击?

如何处理ImeOptions的完成按钮单击?

慕盖茨4494581 2019-11-11 10:42:31
我在一个EditText地方设置以下属性,以便当用户单击EditText时可以在键盘上显示完成按钮。editText.setImeOptions(EditorInfo.IME_ACTION_DONE);当用户单击屏幕键盘上的完成按钮(完成输入)时,我想更改RadioButton状态。从屏幕键盘上击中完成按钮时,我该如何跟踪?
查看完整描述

3 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

我最终得到了罗伯茨和基拉格答案的结合:


((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(

        new EditText.OnEditorActionListener() {

    @Override

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

        // Identifier of the action. This will be either the identifier you supplied,

        // or EditorInfo.IME_NULL if being called due to the enter key being pressed.

        if (actionId == EditorInfo.IME_ACTION_SEARCH

                || actionId == EditorInfo.IME_ACTION_DONE

                || event.getAction() == KeyEvent.ACTION_DOWN

                && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

            onSearchAction(v);

            return true;

        }

        // Return true if you have consumed the action, else false.

        return false;

    }

});

更新: 上面的代码有时会两次激活回调。相反,我选择了以下代码,这些代码是从Google聊天客户端获得的:


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

    // If triggered by an enter key, this is the event; otherwise, this is null.

    if (event != null) {

        // if shift key is down, then we want to insert the '\n' char in the TextView;

        // otherwise, the default action is to send the message.

        if (!event.isShiftPressed()) {

            if (isPreparedForSending()) {

                confirmSendMessageIfNeeded();

            }

            return true;

        }

        return false;

    }


    if (isPreparedForSending()) {

        confirmSendMessageIfNeeded();

    }

    return true;

}


查看完整回答
反对 回复 2019-11-11
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

试试这个,它应该可以满足您的需求:


editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

    @Override

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

    if (actionId == EditorInfo.IME_ACTION_DONE) {

       //do here your stuff f

       return true;

    }

    return false;

    } 

});


查看完整回答
反对 回复 2019-11-11
  • 3 回答
  • 0 关注
  • 706 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信