EditText addTextChangedListener only for user input

后端 未结 1 1683
不知归路
不知归路 2021-01-12 14:43

I have an EditText where I listen for changes in text:

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextCh         


        
相关标签:
1条回答
  • 2021-01-12 15:13

    In my opinion there are two possibilities:

    1. Register / Unregister listener
    2. Flag

    Flag example:

    public class MainActivity extends AppCompatActivity{
        boolean automaticChanged = false;
        ToggleButton toggleButton;
        EditText editText;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                //...
                toggleButton.setOnClickListener(onClickListener);
                editText.addTextChangedListener(textWatcher);
                //...
            }
    
            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    if (!automaticChanged) {
                        // do stuff
                    } else {
                        automaticChanged = false;
                    }
                }
            };
    
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    automaticChanged = true;
                    // do stuff
                }
            };
        }
    }
    
    0 讨论(0)
提交回复
热议问题