Question

I like to have text watcher for edit_text inside a button click. Text watcher should work only when the button is clicked. I used the text watcher code inside the button click. But it didn't work like that, it fires when the edit text is changed. I also tried the following code. doesn't work either.

edt_zip.setImeOptions(EditorInfo.IME_ACTION_DONE);

                    edt_zip.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId,
                                KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_DONE) {
                                if (btn_update_reader.getText().toString()
                                        .equals("CANCEL UPDATE")) {
                                    btn_update_reader.setText("UPDATE READER");
                                }
                                return true;
                            }
                            return false;
                        }
                    });
Was it helpful?

Solution

Create a boolean variable to store the text edit status which is modified when the user clicks the button (true/false). Use the boolean to keep the TextWatch from firing. IE:

private lTextWatcherOn = false;  // set in button.OnClick()
public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (lTextWatchOn) {
              // do what is needed
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top