简体   繁体   English

在EditText中按Enter键时停止键盘关闭?

[英]Stop keyboard from closing when Enter is pressed in EditText?

I have an EditText which has the singleLine attribute set to true. 我有一个EditText,其singleLine属性设置为true。 When I press Enter on the keyboard, the keyboard is hidden. 当我按下键盘上的Enter键时,键盘被隐藏。 Is it possible to prevent this? 有可能阻止这种情况吗?

I've been using OnKeyListener which caused this problem. 我一直在使用OnKeyListener导致了这个问题。 Switching to OnEditorActionListener stops the Keyboard from closing when pressing Enter and allows me to have full control of it. 切换到OnEditorActionListener会在按Enter键时停止键盘关闭,并允许我完全控制它。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
                //DO THINGS HERE
                return true;
            }
            return false;
        }
    });

this should help you 这应该对你有帮助

 youredittext.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
         if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
             (keyCode           == KeyEvent.KEYCODE_ENTER)   )
        {               
           // hide virtual keyboard
           InputMethodManager imm = 
              (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.showSoftInputFromInputMethod(edittext.getWindowToken(), 0);
           return true;
        }
        return false;
    }
});

when you press the enter key the inputMethodManager will show the keyboard, if needed. 当您按下回车键时,inputMethodManager将显示键盘(如果需要)。

hope this will solve your problem :) 希望这会解决你的问题:)

edit: if this will not work try use the event.getKeyCode() in the secend part of the if statment edit II: sorry, i read it wrong, i fixed it now try this one. 编辑:如果这不起作用尝试在if语句编辑II的secend部分使用event.getKeyCode():对不起,我读错了,我现在修复它试试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM