Android: Force EditText to remove focus? [duplicat

2019-01-03 20:32发布

This question already has an answer here:

I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?

19条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-03 21:13
EditText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(URLText.getWindowToken(), 0); 
                    EditText.setFocusable(false);
                    EditText.setFocusableInTouchMode(true);
                    return true;
                } else {
                    return false;
                }
            }
        });
查看更多
登录 后发表回答