簡體   English   中英

在EditText中按Enter鍵時停止鍵盤關閉?

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

我有一個EditText,其singleLine屬性設置為true。 當我按下鍵盤上的Enter鍵時,鍵盤被隱藏。 有可能阻止這種情況嗎?

我一直在使用OnKeyListener導致了這個問題。 切換到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;
        }
    });

這應該對你有幫助

 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;
    }
});

當您按下回車鍵時,inputMethodManager將顯示鍵盤(如果需要)。

希望這會解決你的問題:)

編輯:如果這不起作用嘗試在if語句編輯II的secend部分使用event.getKeyCode():對不起,我讀錯了,我現在修復它試試這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM