android:按下完成键时,软键盘执行操作


Answers:


204

试试这个

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});

20

试试这个

它适用于DONERETURN

EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                            || actionId == EditorInfo.IME_ACTION_DONE) {
                        // Do your action
                        return true;
                    }
                    return false;
                }
            });

0

您抓住KeyEvent,然后检查其键码。FLAG_EDITOR_ACTION用于识别来自输入法已经被自动标记为“下一个”或“完成”的IME的输入键

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

此处找到文档。

第二种方法

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    int result = actionId & EditorInfo.IME_MASK_ACTION;
    switch(result) {
    case EditorInfo.IME_ACTION_DONE:
        // done stuff
        break;
    case EditorInfo.IME_ACTION_NEXT:
        // next stuff
        break;
    }
 }
});

0

试试这个

无论您的键盘显示输入符号还是下一个箭头符号,这都可以使用

YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
            {
                //Perform Action here 
            }
            return false;
        }
    });

如果您面对红线,请执行此操作...通过按alt + enter导入Keyevent和EditorInfo,然后所有错误将其正确删除...。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.