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

前端 未结 5 810
一个人的身影
一个人的身影 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:15

    You can use a flag to differentiate.

    ((EditText) rootView.findViewById(R.id.editText1)).addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }
    
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }
    
            public void afterTextChanged(Editable editable) {
                update(true);
            }
        });            
    
    private boolean updating = false;
    
    private void update(boolean multiply) {
        if(updating){
            return;
        }
        updating = true;
        EditText editText1 = (EditText) getView().findViewById(R.id.editText1);
        editText1.setText("XXX");
        updating = false;
    }
    

提交回复
热议问题