EditText OnKeyListener not working

前端 未结 5 1567
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 17:57

I know this/similar question has been asked before but the solution given is not working for me so I\'m asking again. I tried the solution given in that answer but still my

5条回答
  •  佛祖请我去吃肉
    2020-12-03 18:15

    Finally solved myself by implementing this feature through TextWatcher. The major hurdle was that, I needed to detect backspace press even when there is no character in EditText or at least the end user perceives that there is no character there. The fist thing couldn't be achieved however I did the later one. Following is the detailed solution.

    First of all, I always retained a space ' ' character in my editText.

    editText.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            if(cs.toString().length() == 0)
                editText.setText(" ");
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }
    
        @Override
        public void afterTextChanged(Editable arg0) { }
    
    });
    

    Then I customized EditText to notify me for every cursor position change. This purpose is achieved by overriding onSelectionChanged method of EditText. My customized EditText looks like this.

    public class SelectionEnabledEditText extends EditText {
        public SelectionEnabledEditText(Context context) {
            super(context);
        }
    
        public SelectionEnabledEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SelectionEnabledEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            super.onSelectionChanged(selStart, selEnd);
    
            if(onSelectionChangeListener != null)
                onSelectionChangeListener.onSelectionChanged(selStart, selEnd);
        }
    
        public static interface OnSelectionChangeListener{
            public void onSelectionChanged(int selStart, int selEnd);
        }
    
        private  OnSelectionChangeListener onSelectionChangeListener;
    
        public void setOnSelectionChangeListener(OnSelectionChangeListener onSelectionChangeListener) {
            this.onSelectionChangeListener = onSelectionChangeListener;
        }
    }
    

    Finally, in my activity, I'm listening for cursor position changed event and resetting my cursor position in editText if it's there at the necessary space charatcer start i.e. at 0th index, like this;

    editText.setOnSelectionChangeListener(new SelectionEnabledEditText.OnSelectionChangeListener() {
        @Override
        public void onSelectionChanged(int selStart, int selEnd) {
            if (selEnd == 0) {
                if (editText.getText().toString().length() == 0)
                    editText.setText(" ");
    
                editText.setSelection(1);
            }
        }
    });
    

    Hope this would be helpful in similar situations. Suggestions are welcomed for improvements/optimizations.

提交回复
热议问题