EditText: Differentiate between text change by setText() or by keyboard input

前端 未结 5 847
一个人的身影
一个人的身影 2021-01-04 01:58

I have an EditText View which is edited by setText() from my code and by the user via (soft/hard) keyboard and - if possible by speech input. I wan

5条回答
  •  长情又很酷
    2021-01-04 02:26

    I was having this issue when rotating the device. My editText is inside a dialog. Here's how I solved it:

    editText.addTextChangedListener(
        new TextWatcher() {
    
            @Override
            public void afterTextChanged(Editable s) {
                String newValue = s.toString();
    
               String oldValue = (String) editText.getTag();
    
                if (newValue.length() > 0) {
                    editText.setTag(newValue);
                }
    
                boolean itReallyChanged = oldValue != null && !newValue.equals(oldValue) && !newValue.isEmpty();
                if (itReallyChanged) {
                    // Pretty sure the user genuinely changed this value,
                    // not the device rotation
                }
            }
        }
    );
    

提交回复
热议问题