首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理ImeOptions的“完成”按钮单击?

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

Stack Overflow用户
提问于 2010-01-05 13:31:05
回答 9查看 224.7K关注 0票数 216

我有一个EditText,其中我设置了以下属性,以便当用户单击EditText时可以在键盘上显示done按钮。

代码语言:javascript
复制
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

当用户单击屏幕键盘上的done按钮(完成键入)时,我想要更改RadioButton状态。

从屏幕键盘点击完成按钮时,如何跟踪完成按钮?

EN

回答 9

Stack Overflow用户

发布于 2011-03-21 01:03:29

我最终得到了Roberts和chirags的组合答案:

代码语言:javascript
复制
((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聊天客户端获得的代码:

代码语言:javascript
复制
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;
}
票数 228
EN

Stack Overflow用户

发布于 2011-01-29 16:47:12

试试这个,它应该能满足你的需求:

代码语言:javascript
复制
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;
    } 
});
票数 138
EN

Stack Overflow用户

发布于 2016-10-24 16:13:27

代码语言:javascript
复制
<EditText android:imeOptions="actionDone"
          android:inputType="text"/>

Java代码是:

代码语言:javascript
复制
edittext.setOnEditorActionListener(new OnEditorActionListener() { 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Log.i(TAG,"Here you can write the code");
            return true;
        }    
        return false;
    }
});
票数 47
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2004344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档