How to disable cursor positioning and text selection in an EditText? (Android)

风格不统一 提交于 2019-11-28 07:14:55

I had the same problem. This ended up working for me:

public class CustomEditText extends EditText {

    @Override
    public void onSelectionChanged(int start, int end) {

        CharSequence text = getText();
        if (text != null) {
            if (start != text.length() || end != text.length()) {
                setSelection(text.length(), text.length());
                return;
            }
        }

        super.onSelectionChanged(start, end);
    }

}

This will reset cursor focus to the last position of the text

editText.setSelection(editText.getText().length());

This method will disable cursor move on touch

public class MyEditText extends EditText{

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
         final int eventX = event.getX();
         final int eventY = event.getY();
         if( (eventX,eventY) is in the middle of your editText)
         {
              return false;
         }
         return true;
    }
}

And You can use either the xml attribute

android:cursorVisible

or the java function

setCursorVisible(boolean)

to disable blinking cursor of edittext

Try this:

mEditText.setMovementMethod(null);

It sounds like the best way to do this is to make your own CustomEditText class and override/modify any relevant methods. You can see the source code for EditText here.

public class CustomEditText extends EditText {

    @Override
    public void selectAll() {
        // Do nothing
    }

    /* override other methods, etc. */

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!