How to delete instantly SPACE from an edittext if a user presses the space?

前端 未结 7 2009
星月不相逢
星月不相逢 2020-12-09 22:44

I have an edittext, and a textwatcher that watches if SPACE arrived or not. If its a SPACE I would like to delete that instantly. Or if its a space I want to make sure it do

相关标签:
7条回答
  • 2020-12-09 23:24
    boolean editclicked =false ;
    
    edittext.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            editclicked = false ;
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            editclicked = true;
        }); 
    

    Put this as a separate function:

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (editclicked) {
            if (keyCode == KeyEvent.KEYCODE_SPACE) {
                return false
            }
        } else {
            super.onKeyDown(keyCode, event);
        }
    }
    
    0 讨论(0)
提交回复
热议问题