Disable EditText blinking cursor

前端 未结 10 2001
耶瑟儿~
耶瑟儿~ 2020-11-28 02:27

Does anyone know how to disable the blinking cursor in an EditText view?

10条回答
  •  难免孤独
    2020-11-28 02:56

    In my case, I wanted to enable/disable the cursor when the edit is focused.

    In your Activity:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v instanceof EditText) {
                EditText edit = ((EditText) v);
                Rect outR = new Rect();
                edit.getGlobalVisibleRect(outR);
                Boolean isKeyboardOpen = !outR.contains((int)ev.getRawX(), (int)ev.getRawY());
                System.out.print("Is Keyboard? " + isKeyboardOpen);
                if (isKeyboardOpen) {
                    System.out.print("Entro al IF");
                    edit.clearFocus();
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
                }
    
                edit.setCursorVisible(!isKeyboardOpen);
    
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    

提交回复
热议问题