有人知道如何禁用EditText视图中的闪烁光标吗?


当前回答

在我的例子中,我想在编辑聚焦时启用/禁用游标。

在活动中:

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

其他回答

你可以使用xml属性android:cursorVisible="false"或通过编程方式:

java: view.setCursorVisible(假) 芬兰湾的科特林:视图。isCursorVisible = false

在我的例子中,我想在编辑聚焦时启用/禁用游标。

在活动中:

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

rootLayout.findFocus .clearFocus (());

在kotlin中your_edittext。isCursorVisible = false

改变焦点到另一个视图(例如:任何textview或XML中的线性布局)使用 android: focusableInTouchMode = " true " android: focusable = " true " 在Activity中设置addTextChangedListener为edittext。 然后在aftertextchanged of Edittext上放置Edittext . clearfocus ();

这将在键盘打开时启用光标,在键盘关闭时禁用光标。